|
29 | 29 |
|
30 | 30 | from django.core.files.uploadhandler import FileUploadHandler, SkipFile, StopFutureHandlers, StopUpload, UploadFileException
|
31 | 31 | from django.utils.translation import gettext as _
|
| 32 | +from django.urls import reverse |
32 | 33 |
|
33 | 34 | import hadoop.cluster
|
34 | 35 | from desktop.lib import fsmanager
|
@@ -221,6 +222,43 @@ def close(self):
|
221 | 222 | self._file.close()
|
222 | 223 |
|
223 | 224 |
|
| 225 | +class CustomDocumentsUploadHandler(FileUploadHandler): |
| 226 | + """ |
| 227 | + Delegates the upload handling based on the request URL. |
| 228 | +
|
| 229 | + When the request URL starts with "/desktop/api2/doc/import" (indicating a document |
| 230 | + import), delegate all processing to HDFSfileUploadHandler. |
| 231 | + Otherwise, delegate to FineUploaderChunkedUploadHandler. |
| 232 | + """ |
| 233 | + |
| 234 | + def __init__(self, request, *args, **kwargs): |
| 235 | + super().__init__(request, *args, **kwargs) |
| 236 | + import_path = reverse('import_documents') |
| 237 | + |
| 238 | + if request.path.startswith(import_path): |
| 239 | + self.delegate = HDFSfileUploadHandler(request) |
| 240 | + else: |
| 241 | + self.delegate = FineUploaderChunkedUploadHandler(request, *args, **kwargs) |
| 242 | + |
| 243 | + def new_file(self, field_name, file_name, *args, **kwargs): |
| 244 | + try: |
| 245 | + if hasattr(self.delegate, 'new_file'): |
| 246 | + result = self.delegate.new_file(field_name, file_name, *args, **kwargs) |
| 247 | + except StopFutureHandlers: |
| 248 | + result = None |
| 249 | + return result |
| 250 | + |
| 251 | + def receive_data_chunk(self, raw_data, start): |
| 252 | + if hasattr(self.delegate, 'receive_data_chunk'): |
| 253 | + return self.delegate.receive_data_chunk(raw_data, start) |
| 254 | + return raw_data |
| 255 | + |
| 256 | + def file_complete(self, file_size): |
| 257 | + if hasattr(self.delegate, 'file_complete'): |
| 258 | + return self.delegate.file_complete(file_size) |
| 259 | + return None |
| 260 | + |
| 261 | + |
224 | 262 | class FineUploaderChunkedUploadHandler(FileUploadHandler):
|
225 | 263 | """
|
226 | 264 | A custom file upload handler for handling chunked uploads using FineUploader.
|
|
0 commit comments