def save_xhr(self, request): filename = request.GET["qqfile"] file = SimpleUploadedFile(filename, request.raw_post_data) file.name = normalize("NFC", file.name) expected_length = int(request.environ.get("CONTENT_LENGTH", 0)) if expected_length != file.size: raise Exception("File not fully uploaded") Image.objects.create(image=file, title="uploaded") # TODO: empty title return rc.CREATED
def save_xhr(self, request): filename = request.GET['qqfile'] file = SimpleUploadedFile(filename, request.raw_post_data) file.name = normalize('NFC', file.name) expected_length = int(request.environ.get('CONTENT_LENGTH', 0)) if expected_length != file.size: raise Exception('File not fully uploaded') Image.objects.create( image=file, title='uploaded', # TODO: empty title ) return rc.CREATED
def process_file(file_id): file = core.models.File.objects.select_related('folder').prefetch_related( 'folder__pipelines', 'folder__pipelines__transformations' ).get(pk=file_id) child_list = [] file_metadata = {} for pipeline in file.folder.pipelines.all(): if not pipeline.is_enabled: continue # skip pipeline if file type not supported by it if pipeline.target_type != file.get_file_type() and pipeline.target_type != pipeline.TYPES.ALL: continue # Create Temporary file # TODO: copy file into memory in a more efficient way pipeline_metadata = {} temp_file = SimpleUploadedFile( name=copy.deepcopy(file.name), content=copy.deepcopy(file.content).read(), content_type=copy.deepcopy(file.content_type), ) # Starting Processing of file file_transformation_count = 0 for transformation in pipeline.transformations.filter(type__in=TransformationType.file_types()): transform_type = transformation._type # Skip transformation if it cannot be applied to file of this type if transform_type not in pipeline._target_type.get_file_type(temp_file.content_type).mapping and pipeline.target_type != pipeline.TYPES.ALL: continue temp_file = transformation.process_file(temp_file) file_transformation_count += 1 metadata_transformation_count = 0 for transformation in pipeline.transformations.filter(type__in=TransformationType.metadata_types()): transform_type = transformation._type # Skip transformation if it cannot be applied to file of this type if transform_type not in pipeline._target_type.get_file_type(temp_file.content_type).mapping and pipeline.target_type != pipeline.TYPES.ALL: continue pipeline_metadata.update(transformation.process_metadata(temp_file)) metadata_transformation_count += 1 if metadata_transformation_count > 0: # Append new pipeline metadata to main file file_metadata.update({pipeline.name: pipeline_metadata}) if file_transformation_count > 0: # Add prefix to file name temp_file.name = '%s__%s' % (pipeline.name, temp_file.name) new_file = core.models.File( content=temp_file, parent=file, folder_id=file.folder_id, space_id=file.space_id, pipeline_id=pipeline.id, metadata=pipeline_metadata, ) new_file.save() child_list.append({ 'id': new_file.pk, 'file_name': new_file.name, 'pipeline_id': pipeline.id, 'pipeline_name': pipeline.name, }) # Update main file metadata after finishing processing pipelines if file_metadata != {}: file.metadata = file_metadata file.save(update_fields=['metadata']) return child_list
def ajax_upload(request, pk, type): if request.method == "POST": if request.is_ajax() and False: # the file is stored raw in the request upload = request is_raw = True # AJAX Upload will pass the filename in the querystring if it is # the "advanced" ajax upload try: if 'qqfile' in request.FILES: upload = request.FILES.values()[0] except KeyError: return HttpResponseBadRequest("AJAX request not valid") # not an ajax upload, so it was the "basic" iframe version with # submission via form else: is_raw = False if len(request.FILES) == 1: category_id = request.POST.get('category_id', '') # FILES is a dictionary in Django but Ajax Upload gives the uploaded file an # ID based on a random number, so it cannot be guessed here in the code. # Rather than editing Ajax Upload to pass the ID in the querystring, # observer that each upload is a separate request, # so FILES should only have one entry. # Thus, we can just grab the first (and only) value in the # dict. upload = request.FILES['qqfile'] else: raise Http404("Bad Upload") filename = upload.name # Create a corresponding StandardFile instance for this user # mimetype = MimeType.objects.get(extension='jpg') file_contents = SimpleUploadedFile(upload.name, upload.read()) category_status = 0 registration_id = 0 if type == "1": registration = get_object_or_404(Registration, pk=pk) registration_id = registration.pk category = get_object_or_404(AttachmentCategory, pk=category_id) student_name = registration.costumer.get_full_name() filename = str( registration.pk) + '-' + student_name + '-' + category.name filename = filename.replace(' ', '_') object = Attachment.objects.create( registration=registration, category=category, name=filename, file=file_contents, ) category_status = category.status file_url = settings.MEDIA_URL + str(pk) + "/" + filename filename = settings.MEDIA_ROOT + "/" + str(pk) + "/" + filename if type == "2": object = get_object_or_404(Profile, pk=pk) object.profile_picture = file_contents object.save() file_url = object.profile_picture.url filename = settings.MEDIA_ROOT + "/" + str(pk) + "/" + filename if type == "3": object = MessageAttachment.objects.create(document=file_contents, name=filename) file_url = settings.MEDIA_URL + str(pk) + "/" + filename filename = ATTACHMENT_UPLOAD_ROOT + "/" + filename if type == "4": registration = get_object_or_404(Registration, pk=pk) object = get_object_or_404(Registration, pk=pk) object.profile_picture = file_contents object.save(update_fields=["profile_picture"]) file_url = settings.MEDIA_URL + 'registration_attachments/' + str( pk) + "/" + filename filename = settings.MEDIA_ROOT + "/" + str(pk) + "/" + filename RegistrationActivities.objects.create(registration=registration, creator=request.user, type=9) if type == "5": object = get_object_or_404(Event, pk=pk) object.image = file_contents object.save() file_url = settings.MEDIA_URL + str(pk) + "/" + filename filename = settings.MEDIA_ROOT + "/" + str(pk) + "/" + filename if type == "6": custom_name = request.POST.get('custom_name', None) if custom_name: format = filename.split('.')[-1] filename = custom_name + '.' + format filename = filename.replace(' ', '_') file_contents.name = filename profile = get_object_or_404(Profile, pk=pk) object = ProfileAttachment.objects.create(profile=profile, document=file_contents, name=filename) file_url = reverse('registers:profile_attachment', args=[object.pk]) if type == "7": custom_name = request.POST.get('custom_name', None) if custom_name: filename = custom_name filename = filename.replace(' ', '_') file_contents.name = filename user = get_object_or_404(User, pk=pk) object = WireTransferTicket.objects.create(user=user, document=file_contents, name=filename) file_url = settings.MEDIA_URL + str(pk) + "/" + filename # save the file # save_upload(upload, filename, is_raw) # let Ajax Upload know whether we saved it or not import json ret_json = { 'success': True, 'file_id': object.id, 'file_url': file_url, 'category_status': category_status, 'registration_id': registration_id, 'filename': filename } return HttpResponse(json.dumps(ret_json), status=200)