From: Maciej Tronowski Date: Wed, 1 Apr 2015 15:29:23 +0000 (+0200) Subject: upload handler for streamed files upload to ftp X-Git-Tag: v1.0~99 X-Git-Url: http://mmka.chem.univ.gda.pl/gitweb/?a=commitdiff_plain;ds=sidebyside;h=b8b1ffd47d21234b55f821c9ddcf69191cb05392;p=qcg-portal.git upload handler for streamed files upload to ftp --- diff --git a/filex/uploadhandler.py b/filex/uploadhandler.py new file mode 100644 index 0000000..20a1339 --- /dev/null +++ b/filex/uploadhandler.py @@ -0,0 +1,37 @@ +from django.core.files.uploadedfile import UploadedFile +from django.core.files.uploadhandler import FileUploadHandler, StopUpload, StopFutureHandlers + +from filex.ftp import FTPOperation + + +class FtpUploadHandler(FileUploadHandler): + ftp = None + + def new_file(self, file_name, *args, **kwargs): + super(FtpUploadHandler, self).new_file(file_name, *args, **kwargs) + + # TODO limit to selected request.path + # TODO validate host and path + host = self.request.GET.get('host') + path = self.request.GET.get('path') + + if self.request.user.is_anonymous() or not host or not path: + raise StopUpload(connection_reset=True) + + if self.ftp is None: + self.ftp = FTPOperation(self.request.session['proxy'], self.chunk_size) + + self.ftp.put('gsiftp://' + host + path + self.file_name) + + StopFutureHandlers() + + def receive_data_chunk(self, raw_data, start): + self.ftp.stream.put(raw_data) + + def file_complete(self, file_size): + # signal end of data and wait for finish + self.ftp.stream.put(None) + self.ftp.wait() + + return UploadedFile(name=self.file_name, size=file_size, charset=self.charset, + content_type=self.content_type, content_type_extra=self.content_type_extra)