230
228
'size': pybb_settings.ATTACHMENT_SIZE_LIMIT/1024/1024,
233
232
# Checks by file extension
234
ext = attachment.name.rpartition('.')
233
splitted_fn = attachment.name.rsplit('.', maxsplit=2)
234
print("Franku ext: ", splitted_fn)
235
if len(splitted_fn) == 1:
236
236
# Not sure if we need this
237
237
raise ValidationError(
238
238
'We do not allow uploading files without an extension.'
241
ext = splitted_fn[-1]
242
242
if not ext in settings.ALLOWED_EXTENSIONS:
243
243
raise ValidationError(
244
244
'This type of file is not allowed.'
247
249
raise ValidationError(
248
250
'This seems to be a widelands map file. Please upload \
249
251
it at our maps section.'
254
# Widelands savegame (*.wgf) and widelands replay (*.wrpl.wgf)
256
if ext == 'wgf' and not splitted_fn[-2] == 'wrpl':
252
257
if not _zip_contains(['/binary/', '/map/', '/minimap.png', '/preload']):
253
258
raise ValidationError(
254
259
'This is not a valid widelands savegame.'
264
raise ValidationError(
265
'This file is part of a replay. Please zip it together with \
266
the corresponding .wrpl.wgf file and upload again.'
258
271
raise ValidationError(
259
272
'This is not a valid zip file.'
276
wai = configparser.ConfigParser()
278
wai.read(tmp_file_path)
279
wai_sections = wai.sections()
280
if len(settings.ALLOWED_WAI_SECTIONS) == len(wai_sections):
281
for section in settings.ALLOWED_WAI_SECTIONS:
282
if section not in wai_sections:
287
raise ValidationError(
288
'This not a valid wai file.'
262
291
# Checks by MimeType
263
292
# Get MIME-Type from python-magic
264
293
magic_mime = magic.from_file(tmp_file_path, mime=True)
265
294
magic_mime = _split_mime(magic_mime)
266
295
send_mime = _split_mime(attachment.content_type)
297
# Check for valid image file. Use te mime-type provided by python-magic,
298
# because for a renamed image the wrong mime-type is send by the browser.
299
if magic_mime['maintype'] == 'image':
301
raise ValidationError(
302
'This is not a valid image: %(file)s',
303
params={'file': attachment.name}
268
306
# Compare Mime type send by browser and Mime type from python-magic.
269
307
# We only compare the main type (the first part) because the second
270
308
# part may not be recoginzed correctly. E.g. for .lua the submitted
271
309
# type is 'text/x-lua' but 'x-lua' is not official at all. See:
272
310
# https://www.iana.org/assignments/media-types/media-types.xhtml
273
if not _values_even(magic_mime['maintype'], send_mime['maintype']):
274
raise ValidationError(
275
'The file %(file)s looks like %(send_mime)s, \
276
but we think it is %(magic_mime)s',
278
'file': attachment.name,
279
'send_mime': send_mime['maintype'],
280
'magic_mime': magic_mime['maintype'],
283
# Check for valid image file. Use te Mime-Type provided by python-magic!
284
if magic_mime['maintype'] == 'image':
311
# Unrecoginzed extension are always send with mime type
312
# 'application/octet-stream'. Skip if we know them.
313
if not ext in settings.SKIP_MIME_EXTENSIONS:
314
if not magic_mime['maintype'] == send_mime['maintype']:
286
315
raise ValidationError(
287
'This is not a valid image: %(file)s',
288
params={'file': attachment.name}
316
'The file %(file)s looks like %(send_mime)s, \
317
but we think it is %(magic_mime)s',
319
'file': attachment.name,
320
'send_mime': send_mime['maintype'],
321
'magic_mime': magic_mime['maintype'],