fix: update file upload handling and improve extension validation in Upload class

1 parent 5991130f
......@@ -492,6 +492,7 @@ class Upload(SaveFile):
return fullpath
def save(self, request, name, exts=None, filename=None):
"""Single file upload"""
input_file = request.POST[name].file
ext = get_ext(request.POST[name].filename)
ext = ext and ext.lower() or None
......@@ -502,16 +503,30 @@ class Upload(SaveFile):
head, filename = os.path.split(filename)
return filename
def save_fp(self, upload):
if 'fp' not in upload or upload['fp'] == b'' or not upload['fp'] or not \
upload['size']:
if "filename" in upload:
return upload['filename']
return
filename = upload['filename']
input_file = upload['fp']
ext = get_ext(filename)
filename = self.save_to_file(input_file, ext)
def save_fp(self, upload, exts=None, filename=None):
"""Object FieldStorage"""
if isinstance(upload, dict):
if 'fp' not in upload or upload['fp'] == b'' or not upload['fp'] or not \
upload['size']:
if "filename" in upload:
return upload['filename']
return
file_name = upload['filename']
input_file = upload['fp']
else:
if not hasattr(upload, "fp") or upload.fp == b'' or not upload.fp or not \
upload.bytes_read:
if upload.filename:
return upload.filename
return
file_name = upload.filename
input_file = upload.fp
ext = get_ext(file_name)
if exts and ext not in exts:
raise InvalidExtension(exts)
filename = self.save_to_file(input_file, ext, filename=f"{filename}{ext}" if filename else None)
return os.path.split(filename)[1]
def saves(self, uploads):
......
......@@ -61,7 +61,7 @@ NOPEL.extend([('tahun', 4, 'N'),
('bundel', 4, 'N'),
('urut', 3, 'N')])
NOPELDET = list(NOPEL)
NOPELDET.extend(list(NOP))
NOPELDET.extend(list(SPPT))
AGENDA = [
('kd_seksi', 2, 'N'),
......@@ -96,7 +96,7 @@ class SetFixLength(FixLength):
class BaseFixLength(SetFixLength):
def __init__(self, raw):
super(BaseFixLength, self).__init__(raw, self.get_structure())
raw = re.sub("\D", "", raw)
raw = re.sub(r"\D", "", raw)
self.set_raw(raw)
def get_structure(self):
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!