def test_create_item_pictures__failure_not_an_image(self): user = self.create_user(superuser=True) test_image_path = os.path.join('.', 'tests', 'images', 'not_an_image.log') item = self.create_item() picture_data = { 'title': 'Example image', 'file': FileStorage(open(test_image_path, 'rb')), } resp = self.open_with_auth('/items/{}/pictures'.format(item.uuid), 'post', user.email, 'p4ssw0rd', data=picture_data, content_type='multipart/form-data') assert resp.status_code == BAD_REQUEST
def test_workflow_insufficient_founds_error(self): """ Check the endpoint returns a insufficient founds workflow response (HTTP 409) """ workflow_json = json.dumps(WORKFLOW_MOCK).encode("utf-8") new_patch_dict = { 'app.api.controllers.workflow_process.get_account_balance': { 'return_value': { 'user_id': '105398891', 'balance': 110000, 'account_number': '000101' } }, 'app.api.controllers.workflow_process.deposit_money': { 'return_value': { 'balance': 1 } }, 'app.api.controllers.workflow_process.withdraw': { 'return_value': { 'balance': 23000000000 } } } with Contexter(*self.get_patches(new_patch_dict)): workflow_data = FileStorage( stream=io.BytesIO(workflow_json), filename="workflow_data.json", content_type="application/json", ) response = self.http_request.post(workflow_data) self.json_structure_response_code_assert(409, response) data = deserialize_json(response.data) messages = data['message'] error = messages[len(messages) - 1] self.assertEqual( error, 'ERROR MESSAGE: Insufficient funds to perform a 30 USD withdrawal, account_number 000101.' )
def send_message_with_attach(reference, html_context, msg, user_connected=None): invoice_link = "" recap_filename = "Ref_" + reference + ".pdf" convert_html_to_pdf(html_context, recap_filename) with flask.current_app.open_resource(reference_path_in_sources + recap_filename) as fp: msg.attach(reference_path_in_sources + recap_filename, "application/pdf", fp.read()) if user_connected: with flask.current_app.open_resource(reference_path_in_sources + recap_filename, 'rb') as fp: file = FileStorage(fp) user_id = user_connected['id'] fileStorage_key = user_connected['fileStorage_key'] invoice_link = cloudinary.uploader.upload( file, public_id=fileStorage_key + str(user_id) + file.filename.split(".")[0], folder=CLOUD_INVOICE + "/" + fileStorage_key + str(user_id) )['secure_url'] os.remove(os.path.join(flask.current_app.root_path, reference_path_in_sources + recap_filename)) send_message_to_user(msg) return invoice_link
class TestLocalStorage: subject = LocalStorage() fake_file_storage = FileStorage(stream=BytesIO(b'Some content')) def test_store_byte_string_does_not_throw_exception_when_used(self): self.subject.store_byte_string('Blah', b'Some content', '.') os.remove(os.path.join(self.subject.local_upload_directory, 'Blah')) def test_store_byte_string_writes_file_locally(self): self.subject.store_byte_string('test-file.csv', b'Some content', 'subdir') assert os.path.exists( os.path.join(self.subject.local_upload_directory, 'subdir', 'test-file.csv')) # cleanup os.remove( os.path.join(self.subject.local_upload_directory, 'subdir', 'test-file.csv')) os.rmdir(os.path.join(self.subject.local_upload_directory, 'subdir')) assert not (os.path.exists( os.path.join(self.subject.local_upload_directory, 'subdir', 'test-file.csv'))) def test_get_retrieves_locally_stored_files(self): directory_path = os.path.join(self.subject.local_upload_directory, 'other-subdir') pathlib.Path(directory_path).mkdir() try: with open(os.path.join(directory_path, 'some-file.txt'), 'w') as file: file.write('some text') file.close() assert self.subject.get('some-file.txt', 'other-subdir') == 'some text' finally: # cleanup os.remove(os.path.join(directory_path, 'some-file.txt')) os.rmdir(directory_path) assert not (os.path.exists( os.path.join(directory_path, 'some-file.txt')))
def parse_parts(self, file, boundary, content_length): """Generate ``('file', (name, val))`` and ``('form', (name, val))`` parts. """ in_memory = 0 for ellt, ell in self.parse_lines(file, boundary, content_length): if ellt == _begin_file: headers, name, filename = ell is_file = True guard_memory = False filename, container = self.start_file_streaming( filename, headers, content_length) _write = container.write elif ellt == _begin_form: headers, name = ell is_file = False container = [] _write = container.append guard_memory = self.max_form_memory_size is not None elif ellt == _cont: _write(ell) # if we write into memory and there is a memory size limit we # count the number of bytes in memory and raise an exception if # there is too much data in memory. if guard_memory: in_memory += len(ell) if in_memory > self.max_form_memory_size: self.in_memory_threshold_reached(in_memory) elif ellt == _end: if is_file: container.seek(0) yield ('file', (name, FileStorage(container, filename, name, headers=headers))) else: part_charset = self.get_part_charset(headers) yield ('form', (name, b''.join(container).decode( part_charset, self.errors)))
def post_multipart_form( url, form_data, file ): """Post the multipart/form. Must be called within an app_context()""" with open( file[ 'path' ], 'rb' ) as file_pointer: png_file_storage = FileStorage( stream=file_pointer, filename=file[ 'name' ], content_type=file[ 'content_type' ] ) files = { "file": ( png_file_storage.filename, png_file_storage.stream, png_file_storage.mimetype ) } response = requests.post( url, data=form_data, files=files, headers=HEADERS ) print( 'response.status_code: {}'.format( response.status_code ) )
def success(): if request.method == 'POST': if 'image' not in request.files: flash('No file part') return redirect('/') image1 = request.files['image'] print(image1) try: from werkzeug.datastructures import FileStorage image1 = FileStorage(filename=image1.filename) x = image1.filename.split('.')[-1] print(x) if x in EXTENSIONS: filename = secure_filename(image1.filename) path = os.path.join(MAIN, UPLOAD_FOLDER, filename) print(path) request.files['image'].save(path) model = tf.keras.models.load_model('best_model.h5') img = image.load_img(path, target_size=(200, 200)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) img = np.vstack([x]) classes = model.predict(img) for y in classes: lotus = y[0] rose = y[1] sunflower = y[2] return render_template("success.html", name=image1.filename, lotus=lotus, rose=rose, sunflower=sunflower, path=path) else: print("Error occurred") flash("Wrong format or empty file") time.sleep(1) return redirect('/') except Exception as e: print(e) return redirect('/')
def uploads_endpoint(request): """ Endpoint for file uploads """ username = request.matchdict["username"] requested_user = LocalUser.query.filter( LocalUser.username == username).first() if requested_user is None: return json_error("No such 'user' with id '{0}'".format(username), 404) if request.method == "POST": # Ensure that the user is only able to upload to their own # upload endpoint. if requested_user.id != request.user.id: return json_error("Not able to post to another users feed.", status=403) # Wrap the data in the werkzeug file wrapper if "Content-Type" not in request.headers: return json_error( "Must supply 'Content-Type' header to upload media.") mimetype = request.headers["Content-Type"] if "X-File-Name" in request.headers: filename = request.headers["X-File-Name"] else: filenames = sorted(mimetypes.guess_all_extensions(mimetype)) if not filenames: return json_error('Unknown mimetype: {}'.format(mimetype), status=415) filename = 'unknown{0}'.format(filenames[0]) file_data = FileStorage(stream=io.BytesIO(request.data), filename=filename, content_type=mimetype) # Find media manager entry = new_upload_entry(request.user) entry.media_type = IMAGE_MEDIA_TYPE return api_upload_request(request, file_data, entry) return json_error("Not yet implemented", 501)
def post(self): data = reqparse.RequestParser() data.add_argument("code", type=str) data.add_argument("phone", type=str) data.add_argument("password", type=str) data.add_argument("nickname", type=str) data.add_argument("img_url", type=str) code = data.parse_args()["code"] phone = data.parse_args()["phone"] password = data.parse_args()["password"] nickname = data.parse_args()["nickname"] img_url = data.parse_args()["img_url"] if img_url: img = requests.get(img_url).content openid = get_openid(code) if not openid: return general_response(err_code=201, status_code=400) if not (code and phone and password and nickname and img): return general_response(err_code=101, status_code=400) elif get_user_shop(openid=openid): return general_response(err_code=102, status_code=403) elif get_user_shop(phone=phone): return general_response(err_code=103, status_code=403) else: user = user_shop(openid=openid, phone=phone, password=password, nickname=nickname) if add_in_db(user): storage = FileStorage(stream=BytesIO(img), content_type="image/jpeg") filename = str(user.id) + user.nickname + ".jpg" storage.save("app/static/user_shop_head/" + filename) user.head_image_name = filename update_in_db(user) token = user.generate_auth_token() return general_response(token=token) else: return general_response(err_code=602, status_code=406)
def extract_content(self, beautsoup): """converts html bookmark url to optimized markdown and saves images""" stripped_tags = ["footer", "nav"] url = self.url.rstrip("/") for tag in stripped_tags: if getattr(beautsoup, tag): getattr(beautsoup, tag).extract() resources = beautsoup.find_all(["a", "img"]) for tag in resources: if tag.name == "a": if tag.has_attr("href") and (tag["href"].startswith("/")): tag["href"] = urljoin(url, tag["href"]) # check it's a normal link and not some sort of image # string returns the text content of the tag if not tag.string: # delete tag tag.decompose() elif tag.name == "img" and tag.has_attr("src"): filename = tag["src"].split("/")[-1] try: filename = filename[ : filename.index("?") ] # remove query parameters except ValueError: pass if tag["src"].startswith("/") or tag["src"].startswith("./"): tag["src"] = urljoin(url, tag["src"]) if current_app.config["SCRAPING_CONF"][ "save_images" ] and valid_image_filename(filename): image = FileStorage( BytesIO(requests.get(tag["src"]).content), filename, name="file" ) saved_to = save_image(image) tag["src"] = "/images/" + saved_to res = html2text(str(beautsoup), bodywidth=0) return res
def test_when_create_post_then_success(upload_mock, session, normal_user_factory, create_categories, add_and_commit): user = normal_user_factory(Region=True, UserProfile=True) add_and_commit([user]) categories = create_categories(PostCategoryEnum.get_dict()) # 실제 업로드 확인하려면 아래 경로에 이미지 첨부하고 patch 데코레이터 제거한 뒤 실행. file = FileStorage( stream=io.BytesIO(b"aaa"), filename="C:/project/rabbit/app/extensions/utils/a.jpg", content_type="multipart/form-data", ) dto = CreatePostDto( user_id=user.id, title="떡볶이 나눠 먹어요", body="", region_group_id=1, type=PostTypeEnum.ATTACHMENT.value, is_comment_disabled=True, is_deleted=False, is_blocked=False, report_count=0, read_count=0, last_user_action="default", last_admin_action="default", amount=10, unit=PostUnitEnum.UNIT.value, price_per_unit=10000, status=PostStatusEnum.SELLING.value, category_ids=[categories[0].id], file_type=AttachmentEnum.PICTURE.value, files=[file], ) post_entity = CreatePostUseCase().execute(dto=dto).value assert post_entity.title == dto.title assert post_entity.post_like_count == PostLikeCountEnum.DEFAULT.value assert isinstance(post_entity.attachments, list)
def compress_files(media_files): """Compress media files Args: media_files (list): A list of dicts Returns: FileStorage: The compressed media file """ logger = app.config['LOGGER'] if not os.path.isdir(app.config['TMP_FOLDER_ROOT_PATH']): os.mkdir(app.config['TMP_FOLDER_ROOT_PATH']) logger.debug('The root folder was created in {0}'.format( app.config['TMP_FOLDER_ROOT_PATH'])) tmp_folder = TemporaryDirectory(prefix=app.config['TMP_FOLDER_PREFIX']) logger.debug('Temp folder: {}'.format(tmp_folder.name)) zip_filename = '{0}{1}'.format(uuid.uuid4().hex, app.config['TMP_DEFAULT_COMPRESS_EXT']) zip_filepath = os.path.join(app.config['TMP_FOLDER_ROOT_PATH'], zip_filename) zip_file = ZipFile(zip_filepath, 'x', compression=ZIP_BZIP2, compresslevel=9) for media in media_files: media_filename = hash_filename(media.filename) media_path = os.path.join(tmp_folder.name, media_filename) media.save(media_path) zip_file.write(media_path, media_filename) zip_file.close() tmp_folder.cleanup() compressed_file = open(zip_filepath, 'rb') headers = Headers() headers.add('Content-Type', app.config['TMP_DEFAULT_COMPRESS_CONTENT_TYPE']) return FileStorage(filename=zip_filename, stream=compressed_file.read(), headers=headers)
def upload_profile(user_id, img): file_rename = str(user_id) + "_" + str(datetime.datetime.now().strftime('-%Y-%m-%d-%H-%M-%S')) file_rename_suffix = file_rename + '.jpg' img_fs = FileStorage( stream=BytesIO(img), filename=file_rename_suffix) filename = profile.save(img_fs, name=file_rename_suffix) # pdb.set_trace() file_url = profile.url(filename) basename = profile.get_basename(filename) path = profile.path(filename) new_filename = compress_image(file_rename, path) return new_filename
def test_maven_parser_output_files_transitive_dependencies(): """Test maven parser.""" with (Path(__file__).parent / "files/transitive-dependencies.txt").open('rb') as f: filename = 'transitive-dependencies.txt' # the method MavenParser returns two values: # 1) direct dependencies # 2) transitive dependencies r, t = MavenParser.parse_output_files( [FileStorage(f, filename=filename)]) # check the existence of both sets assert r is not None assert t is not None assert isinstance(r, set) assert isinstance(t, set) # check the returned values assert r == set() assert "maven:org.apache.geronimo.specs:geronimo-javamail_1.4_spec:1.5" in t
def __get_files(info: blackboard.SubmissionInfo) -> t.List[FileStorage]: files = [] for blackboard_file in info.files: if isinstance(blackboard_file, blackboard.FileInfo): name = blackboard_file.original_name bb_file = bb_tree.lookup_direct_child(blackboard_file.name) if not isinstance(bb_file, ExtractFileTreeFile): raise AssertionError( 'File {} was not a file but instead was {}'.format( blackboard_file.name, bb_file)) stream = bb_file.backing_file.open() else: name = blackboard_file[0] stream = io.BytesIO(blackboard_file[1]) if name == '__WARNING__': name = '__WARNING__ (User)' files.append(FileStorage(stream=stream, filename=name)) return files
def _make_zip(self, project, ty): name = self._project_name_latin_encoded(project) json_task_generator = self._respond_json(ty, project.id) if json_task_generator is not None: datafile = tempfile.NamedTemporaryFile() try: datafile.write(json.dumps(json_task_generator)) datafile.flush() zipped_datafile = tempfile.NamedTemporaryFile() try: _zip = self._zip_factory(zipped_datafile.name) _zip.write(datafile.name, secure_filename('%s_%s.json' % (name, ty))) _zip.close() container = "user_%d" % project.owner_id _file = FileStorage(filename=self.download_name(project, ty), stream=zipped_datafile) uploader.upload_file(_file, container=container) finally: zipped_datafile.close() finally: datafile.close()
def upload_persist(): logger.info("Received upload persist request") job_uid = request.args.get('job_uid', None) provider = request.args.get('provider', None) if job_uid is None or provider is None: return jsonify({ "msg": "Did not provide job_uid or provider", "success": False }) monkeyfs_path = get_local_filesystem_for_provider(provider) create_folder_path = os.path.join(monkeyfs_path, "jobs", job_uid) os.makedirs(create_folder_path, exist_ok=True) with tempfile.NamedTemporaryFile(suffix=".tmp") as temp_file: FileStorage(request.stream).save(temp_file.name) persist_tar = tarfile.open(temp_file.name, "r") persist_tar.extractall(path=create_folder_path) return jsonify({"msg": "Successfully uploaded codebase", "success": True})
def _upload_dataset(client): # prepare dataset mdp_dataset, _ = get_cartpole() csv_path = os.path.join('test_data', 'dataset.csv') export_mdp_dataset_as_csv(mdp_dataset, csv_path) # prepare upload request with open(csv_path, 'rb') as f: data = {'is_image': 'false', 'is_discrete': 'true'} file = FileStorage(stream=f, filename='dataset.csv', content_type='text/csv') data['dataset'] = file # upload res = client.post('/api/datasets/upload', data=data, content_type='multipart/form-data') return res, mdp_dataset
def test_chunked_encoding(monkeypatch, dev_server, send_length): stream, length, boundary = stream_encode_multipart({ "value": "this is text", "file": FileStorage( BytesIO(b"this is a file"), filename="test.txt", content_type="text/plain", ), }) headers = {"content-type": f"multipart/form-data; boundary={boundary}"} if send_length: headers["transfer-encoding"] = "chunked" headers["content-length"] = str(length) client = dev_server("data") # Small block size to produce multiple chunks. conn = client.connect(blocksize=128) conn.putrequest("POST", "/") conn.putheader("Transfer-Encoding", "chunked") conn.putheader("Content-Type", f"multipart/form-data; boundary={boundary}") # Sending the content-length header with chunked is invalid, but if # a client does send it the server should ignore it. Previously the # multipart parser would crash. Python's higher-level functions # won't send the header, which is why we use conn.put in this test. if send_length: conn.putheader("Content-Length", "invalid") conn.endheaders(stream, encode_chunked=True) r = conn.getresponse() data = json.load(r) r.close() assert data["form"]["value"] == "this is text" assert data["files"]["file"] == "this is a file" environ = data["environ"] assert environ["HTTP_TRANSFER_ENCODING"] == "chunked" assert "HTTP_CONTENT_LENGTH" not in environ assert environ["wsgi.input_terminated"]
def share_post(post_id): image = G_Image.query.get_or_404(post_id) form = ShareForm() if form.validate_on_submit(): if form.reciever_email.data: email_address = form.reciever_email.data msg = Message( 'Shared Image', sender='GalleryOrganizer<*****@*****.**>', recipients=[email_address]) msg.body = 'This Mail is sent by "' + current_user.username + '" from Gallery Organizer' f = FileStorage(filename=image.image_name) final_image = image.image_name image_file = url_for('static', filename='gallery_images/' + image.image_name) print(image_file) with app.open_resource("static/gallery_images/" + image.image_name) as fp: if f.filename.split('.')[1] == 'jpg': msg.attach(image.image_actual_name, "image/jpg", fp.read()) elif f.filename.split('.')[1] == 'jpeg': msg.attach(image.image_actual_name, "image/jpeg", fp.read()) elif f.filename.split('.')[1] == 'png': msg.attach(image.image_actual_name, "image/png", fp.read()) mail.send(msg) flash( '"' + image.image_actual_name + '" has been shared to: "' + email_address + '"', 'success') return redirect(url_for('post', post_id=post_id)) elif request.method == 'GET': return render_template('share_image.html', title='Share', form=form, image=image, legend="Share Image") return render_template('share_image.html', title='Error', form=form, image=image, legend="Share Image")
def upload_photo(): if request.method == 'POST': # TODO probs need to do sessions stuff here #http://werkzeug.pocoo.org/docs/0.11/datastructures/#werkzeug.datastructures.FileStorage # Step 1: check if the post request has the file part -- if doing with a request.files not base64 # encoded string # if 'File' not in request.files: # return jsonify([{'status':400, 'message':'Must attach a file to upload'}]) # Step 1: get the base64 encoded image and decode into a temp file and create a FileStorage object base64ImageUrl = request.form["imagePreviewUrl"] if base64ImageUrl == None or base64ImageUrl == "": return jsonify([{'status':400, 'message':'Must attach a file to upload'}]) urlList = base64ImageUrl.split(",") # grabs just the base64 encoding from the string imageBase64 = urlList[-1] # Grab the other information from the post HouseId = int(request.form['HouseId']) filename = "house_" + str(HouseId) + ".png" with open(filename, "w+b") as fh: fh.write(imageBase64.decode('base64')) RelativePath = FileStorage(fh) print RelativePath # Step 2: upload relative path to database, note - Flask automatically # uploads the image to S3 bucket photo = Photo(HouseId, RelativePath, datetime.now(), datetime.now()) db.session.add(photo) try: db.session.commit() except exc.IntegrityError: return jsonify({'status':400, 'message':'This HouseId is not valid'}) else: # Step 3: delete file once stored os.remove(filename) # Step 4: Return success status return jsonify({'status':200, 'message': 'Your image was successfully saved!'})
def upload_file(): i = 0 file_list = request.files.getlist('file') print("The length of list is: ", len(file_list)) print("in_upload") for uploaded_file in file_list: print("i=") print(i) f = FileStorage(uploaded_file) print("uploaded_file:") print(type(upload_file)) print("fileStorage?:") print(type(f)) print("file_get:") #print(type(request.files.get('file'))) #uploaded_file=request.files['file'] filename = secure_filename(uploaded_file.filename) if filename != '': file_ext = os.path.splitext(filename)[1] print(file_ext) if file_ext not in app.config['UPLOAD_EXTENSIONS']: abort(400) if file_ext == '.csv': print("in_csv") #df = pd.read_csv(request.files.get('file'), header = 0, error_bad_lines=False) df = pd.read_csv(f, header=0, error_bad_lines=False) number_of_split = (int)(len(df.index) / 200000) print(len(df.index)) print("numbers of split:") print(number_of_split) if (number_of_split < 1): number_of_split = 1 for df_part in np.array_split(df, number_of_split): utils.importCSV_API(engine, df_part) else: print("no file") i = i + 1 return render_template('data.html')
def handleFile(fileid: int, filepath): with open(filepath, 'rb') as fp: file = FileStorage(fp) try: str = StringIO(file.stream.readline().decode("utf-8")) except UnicodeError: raise RuntimeError("Bad data encoding or type.") try: df = read_csv(str, sep='[;,|]', engine="python", header=None) except errors.ParserError: raise RuntimeError("Bad file format.") except errors.EmptyDataError: raise RuntimeError("No data to read.") if df.empty: raise RuntimeError("No data to read.") index, titles = next(df.iterrows()) titles = titles.tolist() titles = normalize_keys(titles) # Считываем данные filedata = file.stream.read().decode('utf-8').replace(';', ',')\ .replace('|', ',')\ .replace('\t', ',') try: df = read_csv(StringIO(filedata), sep=',', names=titles) except errors.ParserError: raise RuntimeError( "Error during parsing. Check the correctness of the data in file." ) except errors.EmptyDataError: raise RuntimeError("No data to read.") if df.empty: raise RuntimeError("No data to read.") df['fileid'] = fileid uploadToDB(df)
def test_save_profile_picture(self) -> None: directory = os.path.dirname( u.get_profile_picture_full_path('default.png')) assert len(os.listdir(directory)) == 1 filenames = [] with open('tests/assets/test.jpg', 'rb') as fp: file = FileStorage(fp) filename = u.save_profile_picture(file) filenames.append(filename) assert len(os.listdir(directory)) == 2 for _ in range(5): filename = u.save_profile_picture(file) filenames.append(filename) assert len(os.listdir(directory)) == 7 for full_filename in map(u.get_profile_picture_full_path, filenames): os.remove(full_filename) assert len(os.listdir(directory)) == 1
def test_environ_builder_unicode_file_mix(): for use_tempfile in False, True: f = FileStorage(BytesIO(br"\N{SNOWMAN}"), "snowman.txt") d = MultiDict(dict(f=f, s="\N{SNOWMAN}")) stream, length, boundary = stream_encode_multipart( d, use_tempfile, threshold=150 ) assert isinstance(stream, BytesIO) != use_tempfile _, form, files = parse_form_data( { "wsgi.input": stream, "CONTENT_LENGTH": str(length), "CONTENT_TYPE": f'multipart/form-data; boundary="{boundary}"', } ) assert form["s"] == "\N{SNOWMAN}" assert files["f"].name == "f" assert files["f"].filename == "snowman.txt" assert files["f"].read() == br"\N{SNOWMAN}" stream.close()
def test_successful_song_upload(self): _id = random.randint(1, 1000) my_audio = os.path.join("tests/test_files/test.wav") my_file = FileStorage(stream=open(my_audio, "rb"), filename="test.wav"), user_payload = { 'duration': '36', 'upload_time': '2021-06-29T08:15:27', 'audioFileType': 'song', 'audioFileID': _id, 'name': 'pankha' } # user_payload['file'] = (io.BytesIO(b"abcdef"), 'test_files\a.jpg') user_payload['file'] = my_file headers = {} response = self.app.post('/audio', headers=headers, data=user_payload) print(response.json) self.assertEqual(201, response.status_code)
def _get_csv_data_from_request(self, csv_filename): if csv_filename is None: msg = ("Not a valid csv file for import") raise BulkImportException(gettext(msg), 'error') retry = 0 csv_file = None datafile = self.get_local_csv_import_file_from_s3(csv_filename) if not datafile: return [] csv_file = FileStorage(open(datafile.name, 'r')) if csv_file is None or csv_file.stream is None: msg = ("Unable to load csv file for import, file {0}".format( csv_filename)) raise BulkImportException(gettext(msg), 'error') csv_file.stream.seek(0) csvcontent = io.StringIO(csv_file.stream.read().decode("UTF8")) csvreader = unicode_csv_reader(csvcontent) return list(self._import_csv_tasks(csvreader))
def test_environ_builder_unicode_file_mix(): for use_tempfile in False, True: f = FileStorage(BytesIO(u'\N{SNOWMAN}'.encode('utf-8')), 'snowman.txt') d = MultiDict(dict(f=f, s=u'\N{SNOWMAN}')) stream, length, boundary = stream_encode_multipart( d, use_tempfile, threshold=150) assert isinstance(stream, BytesIO) != use_tempfile _, form, files = parse_form_data({ 'wsgi.input': stream, 'CONTENT_LENGTH': str(length), 'CONTENT_TYPE': 'multipart/form-data; boundary="%s"' % boundary }) strict_eq(form['s'], u'\N{SNOWMAN}') strict_eq(files['f'].name, 'f') strict_eq(files['f'].filename, u'snowman.txt') strict_eq(files['f'].read(), u'\N{SNOWMAN}'.encode('utf-8')) stream.close()
def _make_zip(self, app, ty): name = self._app_name_latin_encoded(app) json_task_generator = self._respond_json(ty, app.id) if json_task_generator is not None: datafile = tempfile.NamedTemporaryFile() try: for line in json_task_generator: datafile.write(str(line)) datafile.flush() zipped_datafile = tempfile.NamedTemporaryFile() try: zip = self._zip_factory(zipped_datafile.name) zip.write(datafile.name, secure_filename('%s_%s.json' % (name, ty))) zip.close() container = "user_%d" % app.owner_id file = FileStorage(filename=self.download_name(app, ty), stream=zipped_datafile) uploader.upload_file(file, container=container) finally: zipped_datafile.close() finally: datafile.close()
def realtime(): if request.method == 'POST': info = "load image file failed" file = request.files['webcam'] filename = 'realtime.jpg' if file: FileStorage(stream=file.save( os.path.join(app.config['UPLOAD_FOLDER'], filename))) facefilename = alignment( os.path.join(app.config['UPLOAD_FOLDER'], filename)) if facefilename == None: info = 'This picture can not find faces, Please change another picture' else: person = classfier.recognition(facefilename) if person == '': info = 'Can not recognize the picture, Please change another picture' else: info = "Person name " + person session['result'] = info return redirect(url_for('realtime')) return render_template('realtime.html')