def save_document(request_file, content_subdir, related_obj, ashash=True): uploadedfile = UploadedFile(request_file) file_content = uploadedfile.read() doc_obj = Document() doc_obj.filehash = md5(file_content).hexdigest() doc_obj.urlencfilename = quote(uploadedfile.name) doc_obj.filename = uploadedfile.name doc_obj.content_type = uploadedfile.file.content_type if ashash: doc_obj.filepath = settings.BASE_DIR + content_subdir + doc_obj.filehash else: doc_obj.filepath = settings.BASE_DIR + content_subdir + doc_obj.filename if related_obj.__class__.__name__.lower() == "queryset": if len(related_obj) == 1: setattr(doc_obj, related_obj[0].__class__.__name__.lower(), related_obj[0]) else: print "ERROR: The queryset object had %s elements to it" % str( len(related_obj)) else: setattr(doc_obj, related_obj.__class__.__name__.lower(), related_obj) doc_obj.save() wfile = open(doc_obj.filepath, "w") wfile.write(file_content) wfile.close()
def save_file(file: UploadedFile) -> (bool, str): format = get_format(file) filename = generate_filename() full_path = INPUT_DIRECTORY + filename + format with open(INPUT_DIRECTORY + filename + format, 'wb+') as f: f.write(file.read()) return full_path, filename
def create_message(user: str, speech_audio_file: UploadedFile) -> Message: """Создать новое сообщение""" new_message: Message = Message.objects.create( speech_audio_file=speech_audio_file, speech_transcript=recognize(speech_audio_file.read()), user=user) return new_message
def _uploaded_file_obj_to_buffer( uploaded_file: UploadedFile, sample_size: int = 8096 ) -> (bytes, bytes): """ Optimize memory usage by only returning the first and last 8096 bytes (default). """ head = uploaded_file.read(sample_size) try: # Try to position to the almost end of the file seek_pos = uploaded_file.seek(-sample_size, os.SEEK_END) if seek_pos == 0: # The file was smaller that the sample size return head, b"" except IOError: return head, b"" # Continue reading from the end position found foot = uploaded_file.read() return head, foot
def add_document(file: UploadedFile, user: User) -> Document: document_hash = store_document(file.read()) obj, _ = Document.objects.update_or_create( hash=document_hash, name=file.name, content_type=file.content_type, size=file.size, user=user, ) return obj
def parse_file(self, file: UploadedFile, _) -> Iterable[Article]: data = file.read() try: for para in self.split_file(data): yield self.parse_document(para) except ApaError: log.error("APA parse attempt failed.") if settings.DEBUG: log.error("The generated HTML can be found in /tmp/apa_unrtf.html") raise
def validate_speechFile(cls, speech_file: UploadedFile) -> None: # pylint: disable=invalid-name """Проверить mimetype - принимать только FLAC/WAVE файлы""" if magic.from_buffer(speech_file.read( cls.MAXIMAL_MAGIC_BYTE_LENGTH_FOR_ACCEPTED_FILETYPES), mime=True) not in [ # pylint: disable=bad-continuation 'audio/flac', 'audio/x-wav', ]: # pylint: enable=bad-continuation raise exceptions.UnsupportedMediaTypeError('Invalid speech file') speech_file.open()
def upload(req): if req.method == 'POST': if 'file' in req.FILES: file = req.FILES['file'] filename = file._name image = UploadedFile.read(file) print image # apps.ImageTrans.getGrayscale(file) image2 = cv2.imread(image,0) print image2 fp = open('%s/%s' % ('image/data', filename) , 'wb') for chunk in file.chunks(): fp.write(chunk) fp.close() return HttpResponse('File Uploaded') return HttpResponse('Failed to Upload File')
def fileupload(request): if request.method == 'POST': cc = request.POST.get('cc'); myexcel = request.FILES['files[]'] excel_obj = UploadedFile(myexcel) workbook = xlrd.open_workbook(file_contents = excel_obj.read()) all_worksheets = workbook.sheet_names() worksheet_name = all_worksheets[0] worksheet = workbook.sheet_by_name(worksheet_name) for rownum in xrange(worksheet.nrows): tmp = [] for entry in worksheet.row_values(rownum): tmp.append(entry) print tmp return JsonResponse({'status': 'fileupload_ok'})
def fileupload(request): if request.method == 'POST': cc = request.POST.get('cc') myexcel = request.FILES['files[]'] excel_obj = UploadedFile(myexcel) workbook = xlrd.open_workbook(file_contents=excel_obj.read()) all_worksheets = workbook.sheet_names() worksheet_name = all_worksheets[0] worksheet = workbook.sheet_by_name(worksheet_name) for rownum in xrange(worksheet.nrows): tmp = [] for entry in worksheet.row_values(rownum): tmp.append(entry) print tmp return JsonResponse({'status': 'fileupload_ok'})
def save_document(request_file, content_subdir, related_obj, ashash = True): uploadedfile = UploadedFile(request_file) file_content = uploadedfile.read() doc_obj = Document() doc_obj.filehash = md5(file_content).hexdigest() doc_obj.urlencfilename = quote(uploadedfile.name) doc_obj.filename = uploadedfile.name doc_obj.content_type = uploadedfile.file.content_type if ashash: doc_obj.filepath = settings.BASE_DIR + content_subdir + doc_obj.filehash else: doc_obj.filepath = settings.BASE_DIR + content_subdir + doc_obj.filename if related_obj.__class__.__name__.lower() == "queryset": if len(related_obj) == 1: setattr(doc_obj, related_obj[0].__class__.__name__.lower(), related_obj[0]) else: print "ERROR: The queryset object had %s elements to it" % str(len(related_obj)) else: setattr(doc_obj, related_obj.__class__.__name__.lower(), related_obj) doc_obj.save() wfile = open(doc_obj.filepath, "w") wfile.write(file_content) wfile.close()
def _read(file: UploadedFile, n=None): """Read the file, guessing encoding if needed""" binary_content = file.read(n) encoding = _get_encoding(file.encoding, binary_content) return binary_content.decode(encoding)
def multiuploader(request, patient_id=None): """ Main Multiuploader module. Parses data from jQuery plugin and makes database changes. """ if request.method == 'POST': log.info('received POST to main multiuploader view') if request.FILES == None: return HttpResponseBadRequest('Must have files attached!') #getting file data for farther manipulations file = request.FILES[u'files[]'] wrapped_file = UploadedFile(file) filename = wrapped_file.name file_size = wrapped_file.file.size log.info ('Got file: "%s"' % str(filename)) log.info('Content type: "$s" % file.content_type') #writing file manually into model #because we don't need form of any type. print request.POST if patient_id=='new': print wrapped_file.read() return HttpResponse(status=204) image = MultiuploaderImage() patient=Patient.objects.get(pk=patient_id) image.filename=str(filename) image.image=file image.key_data = image.key_generate patient.multiuploaderimage_set.add(image) log.info('File saving done') #getting thumbnail url using sorl-thumbnail if 'image' in file.content_type.lower(): im = get_thumbnail(image, "80x80", quality=50) thumb_url = im.url else: thumb_url = '' #settings imports try: file_delete_url = settings.MULTI_FILE_DELETE_URL+'/' file_url = settings.MULTI_IMAGE_URL+'/'+image.key_data+'/' except AttributeError: file_delete_url = 'multi_delete/' file_url = 'multi_image/'+image.key_data+'/' """ is actually: [{"name": "Screenshot from 2012-11-14 16:17:46.png", "url": "multi_image/95925526541943247735000327303075602114185579370918344597903504067450818566531/", "thumbnail_url": "/media/cache/f8/bd/f8bd83aadeba651ff9c040bb394ce117.jpg", "delete_type": "POST", "delete_url": "multi_delete/7/", "size": 38520}] should be: {"files":[{"url":"http://jquerey-file-upload.appspot.com/AMIfv9734HSTDGd3tIybbnKVru--IjhjULKvNcIGUL2lvfqA93RNCAizDbvP-RQJNbh-N9m8UXsk-90jFFYSp8TlbZYhEcNN6Vb9HzQVQtdmF83H6sE_XkdnlI2V8lHX5V3Y4AamdX6VMbAt9sNWNx2BVGzhTfAYkRLYmRE1VzzWSe9C8c8Fu8g/Screenshot%20from%202012-11-14%2016%3A17%3A46.png","thumbnail_url":"http://lh5.ggpht.com/fcjVNT6qUGoMDtqqaNDNtU4mghy34qlzfj2GujikLgC7Nj5Bs4LUT_DWG_Q8OWujqvYHsKbeQ9pkvoAW4WiaubmqQxobIPyt=s80","name":"Screenshot from 2012-11-14 16:17:46.png","type":"image/png","size":38520,"delete_url":"http://jquery-file-upload.appspot.com/AMIfv9734HSTDGd3tIybbnKVru--IjhjULKvNcIGUL2lvfqA93RNCAizDbvP-RQJNbh-N9m8UXsk-90jFFYSp8TlbZYhEcNN6Vb9HzQVQtdmF83H6sE_XkdnlI2V8lHX5V3Y4AamdX6VMbAt9sNWNx2BVGzhTfAYkRLYmRE1VzzWSe9C8c8Fu8g/Screenshot%20from%202012-11-14%2016%3A17%3A46.png?delete=true","delete_type":"DELETE"}]} """ #generating json response array result = { 'files': [ {"name":filename, "size":file_size, "url":file_url, "thumbnail_url":thumb_url, "delete_url":file_delete_url+str(image.pk)+'/', "delete_type":"POST",} ] } response_data = simplejson.dumps(result) #checking for json data type #big thanks to Guy Shapiro if "application/json" in request.META['HTTP_ACCEPT_ENCODING']: mimetype = 'application/json' else: mimetype = 'text/plain' return HttpResponse(status=204) #response_data, mimetype=mimetype) else: #GETim return HttpResponse('Only POST accepted')
def accept_uploaded_photo(request, album_id): """ Main Multiuploader module. Parses data from jQuery plugin and makes database changes. """ if request.method == 'POST': logid = random.randint(0,1000) log.info('[%s] received POST to main multiuploader view' % logid) if request.FILES == None: return HttpResponseBadRequest('Must have files attached!') #getting file data for farther manipulations file = request.FILES[u'files[]'] wrapped_file = UploadedFile(file) filename = wrapped_file.name file_size = wrapped_file.file.size log.info ('[%s] Got file: "%s"' % (logid, str(filename))) # Write out file to disk as a temp file randnumber = logid # use the random number here too temp_filename = '%stmp%s_%s' % (settings.TEMP_DIRECTORY,randnumber, filename) log.info('[%s] Writing out to: %s' % (logid, temp_filename)) destination = open(temp_filename, 'wb+') if wrapped_file.multiple_chunks(): for chunk in wrapped_file.chunks(): destination.write(chunk) else: destination.write(wrapped_file.read()) destination.close() # Dump out EXIF Tags # im = Image.open(temp_filename) # if hasattr( im, '_getexif' ): # exifinfo = im._getexif() # if exifinfo: # for tag, value in exifinfo.items(): # decoded = ExifTags.TAGS.get(tag, tag) # log.info('Found tag: %s, value: %s' % (decoded,value)) orientation = None date_taken = None # Make full size and thumbsize try: im = Image.open(temp_filename) except IOError: log.info('[%s] Error opening file %s: %s %s' % (logid, temp_filename, e.errno, e)) return HttpResponseBadRequest('Could not read file') if hasattr( im, '_getexif' ): exifinfo = im._getexif() if exifinfo: for tag, value in exifinfo.items(): decoded = ExifTags.TAGS.get(tag, tag) # if decoded != 'MakerNote': # if decoded != 'UserComment': # log.info('Found tag: %s, value: %s' % (decoded,value)) if decoded == 'Orientation': orientation = value log.info('[%s] Found tag: %s, value: %s' % (logid,decoded,value)) elif decoded == 'DateTime': date_taken = datetime.strptime(value, "%Y:%m:%d %H:%M:%S") log.info('[%s] Found tag: %s, value: %s, date_taken=%s' % (logid,decoded,value,date_taken)) # We rotate regarding to the EXIF orientation information if orientation: if orientation == 1: # Nothing log.info('[%s] Orientation: No rotation necessary' % logid) pass elif orientation == 2: # Vertical Mirror log.info('[%s] Orientation: Vertical flip' % logid) im = im.transpose(Image.FLIP_LEFT_RIGHT) elif orientation == 3: # Rotation 180 log.info('[%s] Orientation: Rotation 180' % logid) im = im.transpose(Image.ROTATE_180) elif orientation == 4: # Horizontal Mirror log.info('[%s] Orientation: Horizontal Mirror' % logid) im = im.transpose(Image.FLIP_TOP_BOTTOM) elif orientation == 5: # Horizontal Mirror + Rotation 270 log.info('[%s] Orientation: Flip top bottom, rot 270' % logid) im = im.transpose(Image.FLIP_TOP_BOTTOM).transpose(Image.ROTATE_270) elif orientation == 6: # Rotation 270 log.info('[%s] Orientation: Rotate 270' % logid) im = im.transpose(Image.ROTATE_270) elif orientation == 7: # Vertical Mirror + Rotation 270 log.info('[%s] Orientation: Flip left right, rotate 270' % logid) im = im.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_270) elif orientation == 8: # Rotation 90 log.info('[%s] Orientation: Rotate 90' % logid) im = im.transpose(Image.ROTATE_90) #------------------ # Save the transposed image to disk orig_path = '%stmp%s_mod%s' % (settings.TEMP_DIRECTORY,randnumber, filename) # keep 100% fidelity on the image try: log.info('[%s] Writing corrected photo to path %s' % (logid,orig_path)) im.save(orig_path, "JPEG", quality=100) except IOError: log.info('[%s] Error saving file %s: %s %s' % (logid, orig_path, e.errno, e)) return HttpResponseBadRequest('Could not save file') #------------------ # Save the photo object into the database album = Album.objects.get(id=album_id) photo = Photo() photo.album = album log.info('[%s] Determining photo order' % logid) #------------------ # Determine where in the photo order this picture needs to be photo.order = 0 if date_taken: photo.photodate = date_taken log.info('[%s] Date Taken is %s' % (logid,date_taken)) # Now try to insert the photo by date taken in the order list prev_photo = photo.prev_photo_by_photodate() if prev_photo: log.info('got prev photo. id=%s, photodate=%s, order=%s' % (prev_photo.id,prev_photo.photodate,prev_photo.order)) photo.order = prev_photo.order else: # First in album photo.order = 0 else: # Last in album photo.order = album.photo_set.count() + 1 log.info('[%s] Writing photo entry to database' % logid) #------------------ # Now finally write the entry to the db photo.save() log.info('[%s] Photo object saved. id = %s, order = %s' % (logid, photo.id,photo.order)) #album.reorder_photos() log.info('[%s] Attempting to save file %s to django model id %s' % (logid, orig_path, photo.id)) f = open(orig_path, 'r') photo.filename.save('%s.jpg' % photo.id, File(f)) f.close() log.info('[%s] Cleaning up files' % logid) #clean up temp file unlink(temp_filename) unlink(orig_path) #settings imports file_delete_url = 'multi_delete/' thumbnail_options = dict(size=(200, 200), crop=True) thumb_url = get_thumbnailer(photo.filename).get_thumbnail(thumbnail_options).url #generating json response array result = [] result.append({"name":filename, "size":file_size, "url": thumb_url, "thumbnail_url":thumb_url, "delete_url":'/', "delete_type":"POST",}) response_data = simplejson.dumps(result) #checking for json data type #big thanks to Guy Shapiro if "application/json" in request.META['HTTP_ACCEPT_ENCODING']: mimetype = 'application/json' else: mimetype = 'text/plain' return HttpResponse(response_data, mimetype=mimetype) else: #GET return HttpResponse('Only POST accepted')
def multiuploader(request, patient_id=None): """ Main Multiuploader module. Parses data from jQuery plugin and makes database changes. """ if request.method == 'POST': log.info('received POST to main multiuploader view') if request.FILES == None: return HttpResponseBadRequest('Must have files attached!') #getting file data for farther manipulations file = request.FILES[u'files[]'] wrapped_file = UploadedFile(file) filename = wrapped_file.name file_size = wrapped_file.file.size log.info('Got file: "%s"' % str(filename)) log.info('Content type: "$s" % file.content_type') #writing file manually into model #because we don't need form of any type. print request.POST if patient_id == 'new': print wrapped_file.read() return HttpResponse(status=204) image = MultiuploaderImage() patient = Patient.objects.get(pk=patient_id) image.filename = str(filename) image.image = file image.key_data = image.key_generate patient.multiuploaderimage_set.add(image) log.info('File saving done') #getting thumbnail url using sorl-thumbnail if 'image' in file.content_type.lower(): im = get_thumbnail(image, "80x80", quality=50) thumb_url = im.url else: thumb_url = '' #settings imports try: file_delete_url = settings.MULTI_FILE_DELETE_URL + '/' file_url = settings.MULTI_IMAGE_URL + '/' + image.key_data + '/' except AttributeError: file_delete_url = 'multi_delete/' file_url = 'multi_image/' + image.key_data + '/' """ is actually: [{"name": "Screenshot from 2012-11-14 16:17:46.png", "url": "multi_image/95925526541943247735000327303075602114185579370918344597903504067450818566531/", "thumbnail_url": "/media/cache/f8/bd/f8bd83aadeba651ff9c040bb394ce117.jpg", "delete_type": "POST", "delete_url": "multi_delete/7/", "size": 38520}] should be: {"files":[{"url":"http://jquerey-file-upload.appspot.com/AMIfv9734HSTDGd3tIybbnKVru--IjhjULKvNcIGUL2lvfqA93RNCAizDbvP-RQJNbh-N9m8UXsk-90jFFYSp8TlbZYhEcNN6Vb9HzQVQtdmF83H6sE_XkdnlI2V8lHX5V3Y4AamdX6VMbAt9sNWNx2BVGzhTfAYkRLYmRE1VzzWSe9C8c8Fu8g/Screenshot%20from%202012-11-14%2016%3A17%3A46.png","thumbnail_url":"http://lh5.ggpht.com/fcjVNT6qUGoMDtqqaNDNtU4mghy34qlzfj2GujikLgC7Nj5Bs4LUT_DWG_Q8OWujqvYHsKbeQ9pkvoAW4WiaubmqQxobIPyt=s80","name":"Screenshot from 2012-11-14 16:17:46.png","type":"image/png","size":38520,"delete_url":"http://jquery-file-upload.appspot.com/AMIfv9734HSTDGd3tIybbnKVru--IjhjULKvNcIGUL2lvfqA93RNCAizDbvP-RQJNbh-N9m8UXsk-90jFFYSp8TlbZYhEcNN6Vb9HzQVQtdmF83H6sE_XkdnlI2V8lHX5V3Y4AamdX6VMbAt9sNWNx2BVGzhTfAYkRLYmRE1VzzWSe9C8c8Fu8g/Screenshot%20from%202012-11-14%2016%3A17%3A46.png?delete=true","delete_type":"DELETE"}]} """ #generating json response array result = { 'files': [{ "name": filename, "size": file_size, "url": file_url, "thumbnail_url": thumb_url, "delete_url": file_delete_url + str(image.pk) + '/', "delete_type": "POST", }] } response_data = simplejson.dumps(result) #checking for json data type #big thanks to Guy Shapiro if "application/json" in request.META['HTTP_ACCEPT_ENCODING']: mimetype = 'application/json' else: mimetype = 'text/plain' return HttpResponse(status=204) #response_data, mimetype=mimetype) else: #GETim return HttpResponse('Only POST accepted')
def get_content_type(file: UploadedFile): content_type = magic.from_buffer(file.read(), mime=True) file.seek(0) return content_type