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

1 parent 5991130f
...@@ -492,6 +492,7 @@ class Upload(SaveFile): ...@@ -492,6 +492,7 @@ class Upload(SaveFile):
return fullpath return fullpath
def save(self, request, name, exts=None, filename=None): def save(self, request, name, exts=None, filename=None):
"""Single file upload"""
input_file = request.POST[name].file input_file = request.POST[name].file
ext = get_ext(request.POST[name].filename) ext = get_ext(request.POST[name].filename)
ext = ext and ext.lower() or None ext = ext and ext.lower() or None
...@@ -502,16 +503,30 @@ class Upload(SaveFile): ...@@ -502,16 +503,30 @@ class Upload(SaveFile):
head, filename = os.path.split(filename) head, filename = os.path.split(filename)
return filename return filename
def save_fp(self, upload): 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 \ if 'fp' not in upload or upload['fp'] == b'' or not upload['fp'] or not \
upload['size']: upload['size']:
if "filename" in upload: if "filename" in upload:
return upload['filename'] return upload['filename']
return return
filename = upload['filename'] file_name = upload['filename']
input_file = upload['fp'] input_file = upload['fp']
ext = get_ext(filename) else:
filename = self.save_to_file(input_file, ext) 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] return os.path.split(filename)[1]
def saves(self, uploads): def saves(self, uploads):
......
...@@ -61,7 +61,7 @@ NOPEL.extend([('tahun', 4, 'N'), ...@@ -61,7 +61,7 @@ NOPEL.extend([('tahun', 4, 'N'),
('bundel', 4, 'N'), ('bundel', 4, 'N'),
('urut', 3, 'N')]) ('urut', 3, 'N')])
NOPELDET = list(NOPEL) NOPELDET = list(NOPEL)
NOPELDET.extend(list(NOP)) NOPELDET.extend(list(SPPT))
AGENDA = [ AGENDA = [
('kd_seksi', 2, 'N'), ('kd_seksi', 2, 'N'),
...@@ -96,7 +96,7 @@ class SetFixLength(FixLength): ...@@ -96,7 +96,7 @@ class SetFixLength(FixLength):
class BaseFixLength(SetFixLength): class BaseFixLength(SetFixLength):
def __init__(self, raw): def __init__(self, raw):
super(BaseFixLength, self).__init__(raw, self.get_structure()) super(BaseFixLength, self).__init__(raw, self.get_structure())
raw = re.sub("\D", "", raw) raw = re.sub(r"\D", "", raw)
self.set_raw(raw) self.set_raw(raw)
def get_structure(self): 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!