upload handler for streamed files upload to ftp
authorMaciej Tronowski <mtro@man.poznan.pl>
Wed, 1 Apr 2015 15:29:23 +0000 (17:29 +0200)
committerMaciej Tronowski <mtro@man.poznan.pl>
Wed, 1 Apr 2015 15:29:23 +0000 (17:29 +0200)
filex/uploadhandler.py [new file with mode: 0644]

diff --git a/filex/uploadhandler.py b/filex/uploadhandler.py
new file mode 100644 (file)
index 0000000..20a1339
--- /dev/null
@@ -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)