Skip to content

Commit

Permalink
Add file size check on upload
Browse files Browse the repository at this point in the history
  • Loading branch information
pierotofy committed May 8, 2024
1 parent f0cd13a commit 5375eb8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,14 @@ def upload(self, request, pk=None, project_pk=None):
if len(files) == 0:
raise exceptions.ValidationError(detail=_("No files uploaded"))

task.handle_images_upload(files)
uploaded = task.handle_images_upload(files)
task.images_count = len(task.scan_images())
# Update other parameters such as processing node, task name, etc.
serializer = TaskSerializer(task, data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
serializer.save()

return Response({'success': True}, status=status.HTTP_200_OK)
return Response({'success': True, 'uploaded': uploaded}, status=status.HTTP_200_OK)

@action(detail=True, methods=['post'])
def duplicate(self, request, pk=None, project_pk=None):
Expand Down
4 changes: 4 additions & 0 deletions app/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,7 @@ def get_image_path(self, filename):
return path_traversal_check(p, self.task_path())

def handle_images_upload(self, files):
uploaded = {}
for file in files:
name = file.name
if name is None:
Expand All @@ -1181,6 +1182,9 @@ def handle_images_upload(self, files):
else:
with open(file.temporary_file_path(), 'rb') as f:
shutil.copyfileobj(f, fd)

uploaded[name] = os.path.getsize(dst_path)
return uploaded

def update_size(self, commit=False):
try:
Expand Down
2 changes: 1 addition & 1 deletion app/static/app/js/components/ProjectListItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class ProjectListItem extends React.Component {
}else{
// Check response
let response = JSON.parse(file.xhr.response);
if (response.success){
if (response.success && response.uploaded && response.uploaded[file.name] === file.size){
// Update progress by removing the tracked progress and
// use the file size as the true number of bytes
let totalBytesSent = this.state.upload.totalBytesSent + file.size;
Expand Down

0 comments on commit 5375eb8

Please sign in to comment.