Skip to content

Commit

Permalink
Change upload resource chunk size
Browse files Browse the repository at this point in the history
  • Loading branch information
blagojabozinovski committed Sep 21, 2023
1 parent b6806fe commit 7fb2281
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
31 changes: 19 additions & 12 deletions ckanext/big_resources/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@
from ckan.lib.uploader import ResourceUpload as DefaultResourceUpload
import ckanext.big_resources.views as views
import os
import requests
from requests_toolbelt.multipart import encoder
from ckan.lib.uploader import _copy_file

import ckan.logic as logic
from ckan.common import g, request
from requests_toolbelt.multipart.encoder import (
MultipartEncoder,
MultipartEncoderMonitor,
)


class BigResourcesPlugin(plugins.SingletonPlugin):
Expand All @@ -36,6 +29,21 @@ def get_blueprint(self):
return views.get_blueprints()


def _copy_file_overwriten(input_file, output_file, max_size):

###To do - Implement validator
if len(input_file.read()) > max_size:
raise logic.ValidationError({'upload': ['File upload too large']})
input_file.seek(0)
while True:
# Chunk Size in bytes
data = input_file.read(10000000)

if not data:
break
output_file.write(data)


class ResourceUpload(DefaultResourceUpload):

def upload(self, id, max_size=10):
Expand Down Expand Up @@ -66,11 +74,12 @@ def upload(self, id, max_size=10):
except OSError as e:
# errno 17 is file already exists
if e.errno != 17:
raise
raise

tmp_filepath = filepath + '~'
with open(tmp_filepath, 'wb+') as output_file:
try:
_copy_file(self.upload_file, output_file, max_size)
_copy_file_overwriten(self.upload_file, output_file, max_size)
except logic.ValidationError:
os.remove(tmp_filepath)
raise
Expand All @@ -89,5 +98,3 @@ def upload(self, id, max_size=10):
os.remove(filepath)
except OSError as e:
pass


2 changes: 0 additions & 2 deletions ckanext/big_resources/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def read_file_chunks(path):

filepath = upload.get_path(rsc[u'id'])


return Response(
stream_with_context(read_file_chunks(filepath)),
headers={
Expand All @@ -66,6 +65,5 @@ def read_file_chunks(path):
return h.redirect_to(rsc[u'url'])



def get_blueprints():
return [big_resources]

0 comments on commit 7fb2281

Please sign in to comment.