def upload_image(): try: file = request.files["image#upload"] image = encode_image(file) image_name = str(file.filename) image_type = file.content_type except: return {"error": "given wrong parameter"} ioBufferImage = decode_image(image) image = FileStorage(ioBufferImage, filename=image_name, content_type=image_type) imageNewName = image_name.rsplit('.') imageNewName = imageNewName[0] + "." + datetime.now().strftime( "%m%d%Y_%H%M%S") + "." + imageNewName[1] path = os.path.join(os.path.abspath(UPLOAD_FOLDER), imageNewName) image.save(path) image.close() # return jsonify({}) return jsonify({ 'success': 'upload successful', 'image_details': { 'name': image_name } })
def setSequencesFromFile(self, seqfile: FileStorage, prefix: str): # Store the input in a temporary file for the parser to process tempf, tpath = tempfile.mkstemp( '.' + secure_filename(seqfile.filename).rsplit('.', 1)[1].lower()) seqfile.save(tpath) seqfile.close() objSequences = [] try: session = self.getSession() isProtein = session.loadHostOrganism() != "" # Parse sequence file objSequences = parse(tpath, isProtein, self.getBoostClient(), session.loadHostOrganism(), session.loadJugglingStrategy()) except Exception as e: print(e) return json.jsonify({'error': 'File format not supported'}) finally: # Cleanup os.close(tempf) os.remove(tpath) # Convert [SeqObject] to [SequenceInformation] sequences = sequenceInfoFromObjects(objSequences) # Add specified prefix for seq in sequences: uniqueID = str(SequenceInformation.generateId()) seq.name = uniqueID + "_" + prefix + "_" + seq.name seq.key = uniqueID self.setSequences(sequences) return 'upload successful'
class SaveAttachmentTest(MailgunTestBase): def setUp(self): super(SaveAttachmentTest, self).setUp() (filename, file_stream) = get_attachment() self.attachment = FileStorage(stream=file_stream, filename=filename) def tearDown(self): super(SaveAttachmentTest, self).tearDown() self.attachment.close() def test_fileallowed(self): self.assertTrue(self.mailgun._is_file_allowed('test.txt')) self.assertFalse(self.mailgun._is_file_allowed('bob')) def test_save_attachments(self): testdir = tempfile.mkdtemp() self.attachment.seek(0) filenames = save_attachments([self.attachment], testdir) filenames = [os.path.basename(filename) for filename in filenames] self.assertTrue(set(os.listdir(testdir)) == set(filenames)) self.assertEqual(len(os.listdir(testdir)), 1) shutil.rmtree(testdir) with self.assertRaises(OSError): os.listdir(testdir)
def file2image(file: FileStorage) -> Image.Image: logger.info(f'Got file {file.filename} of {file.mimetype}') file.stream.seek(0) byte_stream = io.BytesIO(file.read()) logger.info(f'File Size: {byte_stream.getbuffer().nbytes}') file.close() image = Image.open(byte_stream) image = image.convert('RGB') return image
def remove_file_storage(file_storage: FileStorage) -> None: """remove the file of a FileStorage""" file_storage.close() try: name = file_storage.stream.name except AttributeError: pass else: if valid_path(name): os.unlink(name)
def store_tgz(bucket_name, prefix, tgz_file: FileStorage): client = _get_minio_client() tar = tarfile.open(fileobj=tgz_file.stream, mode="r:gz") for member in tar.getmembers(): file_ext = os.path.splitext(member.name)[-1].lower() if file_ext in [".yaml", ".yml", ".md", ".py"]: object_name = f"{prefix.rstrip('/')}/{member.name}" f = tar.extractfile(member) client.put_object(bucket_name, object_name, f, f.raw.size) # f.read() f.close() tar.close() tgz_file.close() return True
def file2image(file: FileStorage) -> Image.Image: """ Args: file (FileStorage): a flask file object received from the request Returns: (PIL.Image.Image) """ logger.info(f"Got file {file.filename} of {file.mimetype}") file.stream.seek(0) byte_stream = io.BytesIO(file.read()) logger.info(f"File Size: {byte_stream.getbuffer().nbytes}") file.close() image: Image.Image = Image.open(byte_stream) image: Image.Image = image.convert("RGB") return image
def save_file_from_request(file: FileStorage, file_path: str): """Save file at a specific location. Args: file (FileStorage): File. file_path (str): File name. Raises: PmaApiException: If file saved is 0 bytes """ if not os.path.exists(os.path.dirname(file_path)): os.mkdir(os.path.dirname(file_path)) file.save(file_path) file.close() if os.path.getsize(file_path) == 0: raise PmaApiException('File saved, but was 0 bytes.\n- Path: {}' .format(file_path))
def attach_file(file: FileStorage, station_id): print(file) if file.content_type != 'application/pdf': raise AppException("File is not a valid pdf.") station_attached = StationAttachedFileModel.find_by(station_id=station_id, filename=file.filename, get_first=True) if not station_attached: station_attached = StationAttachedFileModel.create( file.filename, station_id) if station_attached.save(): try: station_attached.write_file(file) except IOError as e: app_logger.error(str(e)) file.close() return response.empty_response()
def test_correct_file(client): file = FileStorage(stream=open('./data/test.csv.gz', 'rb')) response = client.post('/score', data={'userid': '1', 'file': file}) assert response.status_code == 200 file = open("examples/example.csv.gz", "wb") file.write(response.data) file.close() models = pd.read_csv("examples/example.csv.gz") count = models.groupby('medication').count() count_len = len(count) assert count_len == 5 count_no = models[models['score'] == -1].groupby('medication').count() count_no_len = len(count_no) assert count_no_len == 0
def file2audiotensor(file: FileStorage) -> Tensor: """ Reads the FileStorage object into Bytes, then converts it to a normalized torch tensor Args: file: the FileStorage object from flask Request Returns: (Tensor): the normalized tensor representation of the audio """ logger.info(f"Got file {file.filename} of {file.mimetype}") file.stream.seek(0) byte_stream = io.BytesIO(file.read()) logger.info(f"File Size: {byte_stream.getbuffer().nbytes}") file.close() # convert webm to wav process = ( ffmpeg .input('pipe:0') .output('pipe:1', format='wav') .run_async(pipe_stdin=True, pipe_stdout=True, pipe_stderr=True, quiet=True, overwrite_output=True) ) output, err = process.communicate(input=byte_stream.read()) riff_chunk_size = len(output) - 8 # Break up the chunk size into four bytes, held in b. q = riff_chunk_size b = [] for i in range(4): q, r = divmod(q, 256) b.append(r) # Replace bytes 4:8 in proc.stdout with the actual size of the RIFF chunk. riff = output[:4] + bytes(b) + output[8:] data, _ = sf.read(io.BytesIO(riff)) waveform: Tensor = torch.from_numpy(data).float() return waveform.unsqueeze(0)
def prep_capsule(policy_file: FileStorage, data_file: FileStorage) -> str: # NOTE: KEYSERVER prefix is found in https://github.com/TrustedCapsules/optee_app/blob/master/common/capsuleKeys.h # NOTE: Validation in https://github.com/TrustedCapsules/optee_app/blob/master/capsule_gen/cmd/cgen/gen_helper.c capsule_name = 'KEYSERVER_' + str( datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")) + '__' + hex( random.getrandbits(16)) with current_app.app_context(): work_dir = current_app.config['CAPSULE_TEMP_WORK_PATH'] capsule_path = os.path.join(work_dir, capsule_name) os.mkdir(capsule_path) policy_file.save(os.path.join(capsule_path, capsule_name + '.policy')) policy_file.close() data_file.save(os.path.join(capsule_path, capsule_name + '.data')) data_file.close() with open(os.path.join(capsule_path, capsule_name + '.kvstore'), 'w') as file: file.write('location: NSS Lab, Vancouver, BC') file.close() return capsule_name
def test_upload_img(client): ''' Тест на загрузку файла картинки на сервер. Корректный запрос. ''' image_path = os.path.join(os.path.dirname(__file__), 'tmp_img', 'cat4.jpg') img_file = FileStorage( stream=open(image_path, "rb"), filename="cat4.jpg", content_type="image/jpeg", ) response = client.post('/api/v1.0/images', content_type='multipart/form-data', data={'file': img_file, }, ) img_file.close() assert '200' in response.status ''' Некорректный запрос. Такой файл уже существует. ''' img_file = FileStorage( stream=open(image_path, "rb"), filename="cat4.jpg", content_type="image/jpeg", ) response = client.post('/api/v1.0/images', content_type='multipart/form-data', data={'file': img_file, }, ) img_file.close() assert '400' in response.status
def setUp(self): self.app.config['UPLOAD_FOLDER'] = tempfile.mkdtemp() self.upload_folder = pathlib.Path(self.app.config['UPLOAD_FOLDER']) path = pathlib.Path(BASEDIR) path = path / 'testdata' / 'images' / 'testimage1.jpg' imagefile = path imagefile = FileStorage( stream=open(str(path), 'br'), filename=str(path), headers=Headers([('Content-Type', 'image/jpeg')]), ) with patch( target='project.tasks.uploads.celery_make_thumbnail.delay', new=make_thumbnail, ): UploadedImage.bl.save_image( image=imagefile, img_category=UploadedImage.IMG_CATEGORY.other, title='testing image', description='testing image', ) imagefile.close()
def __init__(self, file_storage: FileStorage) -> None: self.file_storage = file_storage file_storage.close() # allow for reading/writing/moving under windows
def write_file(self, file: FileStorage): file_path = os.path.join(self.folder_path, file.filename) if not os.path.isfile(file_path): file.save(file_path) file.close()
def ueditor_upload(): """ueditor接口""" mimetype = 'application/json' result = {} action = request.args.get('action') # 解析JSON格式的配置文件 with open('static/ueditor/php/config.json') as fp: try: # 删除 `/**/` 之间的注释 CONFIG = json.loads(re.sub(r'\/\*.*\*\/', '', fp.read())) except: CONFIG = {} # 初始化时,返回配置文件给客户端 if action == 'config': result = CONFIG # 上传文件 elif action in ('uploadimage', 'uploadvideo', 'uploadfile'): # 图片、文件、视频上传 # if action == 'uploadimage': # fileName = CONFIG.get('imageFieldName') if len(request.files) > 1: result = {'state': '不支持多个文件同时上传'} else: f = request.files.values()[0] file_id = save_gfs_file(resource_gfs, f) f.close() result = { "state": "SUCCESS", "url": resource_gfs_url(file_id), "title": f.filename, "original": f.filename, # "type": "." + f.filename.split('.')[-1], # "size": 7819 } # 涂鸦图片上传 elif action == 'uploadscrawl': base64data = request.form['upfile'] # 这个表单名称以配置文件为准 img = base64.b64decode(base64data) buff = StringIO() buff.write(img) buff.seek(0) # tricky save_gfs_file only accept FileStorage object or something like file_storage = FileStorage(filename='', content_type='image/png', content_length=buff.len, stream=buff) file_id = save_gfs_file(resource_gfs, file_storage) file_storage.close() result = { "state": "SUCCESS", "url": resource_gfs_url(file_id), "title": file_id, "original": file_id } # 抓取远程图片 elif action == 'catchimage': sources = request.form.getlist('source[]') _list = [] for source in sources: f = urllib2.urlopen(urllib2.Request(source, headers={'User-agent': 'Mozilla/5.0'})) buff = StringIO() buff.write(f.read()) buff.seek(0) # tricky save_gfs_file only accept FileStorage object or something like file_storage = FileStorage(headers=f.headers, stream=buff) file_id = save_gfs_file(resource_gfs, file_storage) f.close() file_storage.close() _list.append({ "url": resource_gfs_url(file_id), "source": source, "state": "SUCCESS" }) result = { "state": "SUCCESS", "list": _list } else: result = {'state': '不能识别的action: %s' % action} # todo: 回调参数, 这个不知道有没有用到,从别处拷过来的 if 'callback' in request.args: callback = request.args.get('callback') if re.match(r'^[\w_]+$', callback): result = '%s(%s)' % (callback, result) mimetype = 'application/javascript' res = make_response(result) res.mimetype = mimetype res.headers['Access-Control-Allow-Origin'] = '*' res.headers['Access-Control-Allow-Headers'] = 'X-Requested-With,X_Requested_With' return res else: result = {'state': 'callback参数不合法'} # return jsonify(result) # return json.dumps(result) result = json.dumps(result) # print result res = make_response(result) res.mimetype = 'application/json' res.headers['Access-Control-Allow-Origin'] = '*' res.headers['Access-Control-Allow-Headers'] = 'X-Requested-With,X_Requested_With' return res
def test_simple_answer(db_test_client): from application import db reveal = pytz.utc.localize( datetime.datetime.utcnow()) - datetime.timedelta(minutes=1) teacher = insert_users(db, 1, roles=["TEACHER"])[0] student = insert_users(db, 1, roles=["USER"])[0] c_id, code = db.insert_course(Course("something", "something", reveal), teacher.id) db.enlist_student(code, student.id) f = generate_random_file() file_name = secure_filename(get_random_unicode(30)) werk_file = FileStorage(f, file_name) a_id = db.insert_assignment(teacher.id, c_id, "ksakas", reveal, reveal, [werk_file]) t_id = db.insert_task(teacher.id, a_id, "something", 3, [werk_file]) werk_file.close() files = [] correct_files = FileMultiDict() for _ in range(5): f = generate_random_file() file_name = secure_filename(get_random_unicode(30)) werk_file = FileStorage(f, file_name) files.append(werk_file) correct_files.add_file(file_name, werk_file) description = get_random_unicode(100) db.update_answer(teacher.id, t_id, files, description, reveal=random_datetime()) assignment = db.select_assignment(a_id, task_id=t_id) assert assignment.name == "ksakas" assert len(assignment.tasks) == 1 t = assignment.tasks[0] assert isinstance(t, Task) db.set_task_answer(assignment.tasks[0], for_student=False) assert t.answer is not None assert isinstance(t.answer, Answer) assert t.answer.description == description assert len(t.answer.files) == 5 for db_f in t.answer.files: assert isinstance(db_f, File) c_f = correct_files.get(db_f.name) bin_file, name = db.get_file(db_f.id) assert name == db_f.name, "shouldn't fail... " + str( type(name)) + " " + str(type(db_f.name)) c_f.seek(0) assert bin_file == c_f.read() c_f.close() db.set_task_answer(t, for_student=True) assert t.answer is None
def test_large_answer_insert_and_update(db_test_client): from application import db visible_reveal = pytz.utc.localize( datetime.datetime.utcnow()) - datetime.timedelta(minutes=1) teacher = insert_users(db, 1, roles=["TEACHER"])[0] course_id, _ = db.insert_course( Course("something", "somthing", visible_reveal, visible_reveal), teacher.id) f = generate_random_file() file_name = secure_filename(get_random_unicode(30)) werk_file = FileStorage(f, file_name) assignment_id = db.insert_assignment(teacher.id, course_id, "somthing", random_datetime(), visible_reveal, [werk_file]) werk_file.close() all_answers = [] visible_answers = [] visible_tasks = [] correct_files = FileMultiDict() for _ in range(100): task_id = db.insert_task(teacher.id, assignment_id, get_random_unicode(20), 20, []) hidden = random.randint(0, 1) if not hidden: visible_tasks.append(task_id) reveal = visible_reveal else: reveal = random_datetime() files = [] for i in range(3): f = generate_random_file(length=10) file_name = str(task_id) + "t" + str(i) werk_file = FileStorage(f, file_name) files.append(werk_file) correct_files.add_file(task_id, werk_file, werk_file.filename) desc = get_random_unicode(30) id = db.update_answer(teacher.id, task_id, files, desc, reveal=reveal) answer = Answer(id, desc, reveal.replace(tzinfo=None), db.select_file_details(answer_id=id)) all_answers.append(answer) if not hidden: visible_answers.append(answer) for f in files: f.close() assignment = db.select_assignment(assignment_id, for_student=False) all_db_answers = [] case = unittest.TestCase() assert len(assignment.tasks) == 100 for t in assignment.tasks: assert isinstance(t, Task) db.set_task_answer(t, for_student=False) assert t.answer is not None a = t.answer assert isinstance(a, Answer) assert len(a.files) == 3 correct_filenames = [ file.filename for file in correct_files.getlist(t.id) ] assert len(correct_filenames) == 3 db_filenames = [file.name for file in a.files] case.assertCountEqual(db_filenames, correct_filenames) all_db_answers.append(a) assert len(all_answers) == len(all_db_answers) case.maxDiff = None case.assertCountEqual(all_answers, all_db_answers) assignment = db.select_assignment(assignment_id, for_student=True) assert len(assignment.tasks) == 100 db_visibles = [] for t in assignment.tasks: assert isinstance(t, Task) db.set_task_answer(t, for_student=True) if t.id not in visible_tasks: assert t.answer is None continue assert t.answer is not None db_visibles.append(t.answer) a = t.answer assert isinstance(a, Answer) assert len(a.files) == 3 correct_filenames = [ file.filename for file in correct_files.getlist(t.id) ] assert len(correct_filenames) == 3 db_filenames = [file.name for file in a.files] case.assertCountEqual(db_filenames, correct_filenames) case.assertCountEqual(db_visibles, visible_answers)