def ecc_generate(): if request.method == 'GET': return render_template('ecc/generate/index.html') coef = int(request.form['x-coef']) const = int(request.form['const']) modulo = int(request.form['modulo']) if not is_prime(modulo): return "Error: Modulo must be prime" curve = EllipticCurve(coef, const, modulo) base_x = int(request.form['bpoint-x']) base_y = int(request.form['bpoint-y']) bpoint = Point(base_x, base_y, modulo) if not curve.contains(bpoint): return "Error: The base point is not a point in the curve" private_key = randrange(1, MAX_KEY) public_key = curve.multiply(private_key, bpoint) filename = request.form['filename'] public_file = FileStorage(stream=BytesIO(public_key.print().encode()), filename=filename + '.pub') public_file.save('static/' + public_file.filename) private_file = FileStorage(stream=BytesIO(str(private_key).encode()), filename=filename + '.pri') private_file.save('static/' + private_file.filename) return render_template('ecc/generate/result.html')
def test_handle_post_request_file_unknwon(mock_server_result, tmpdir): # videos m = mock.MagicMock() files = { "file": FileStorage(open(PATH_TO_TEST_VIDEO, "rb"), content_type='video/mkv') } m.files = files m.form = {"fps": 2, "foo": "bar"} split_frames_folder = os.path.join( tmpdir, "{}_split".format(os.path.basename(PATH_TO_TEST_VIDEO))) os.mkdir(split_frames_folder ) # this folder should be deleted by handle post request with mock.patch("mot.serving.inference.request", m): outputs = handle_post_request(upload_folder=str(tmpdir)) assert "error" in outputs assert outputs["error"].endswith("{'unknown': '¯\\\\_(ツ)_/¯'}") # images data = np.ones((300, 200, 3)) filename = "test.jpg" filepath = os.path.join(tmpdir, filename) cv2.imwrite(filepath, data) m = mock.MagicMock() files = { "file": FileStorage(open(filepath, "rb"), content_type='image/jpg') } m.files = files with mock.patch("mot.serving.inference.request", m): outputs = handle_post_request(upload_folder=str(tmpdir)) assert "error" in outputs assert outputs["error"].endswith("{'unknown': '¯\\\\_(ツ)_/¯'}")
def test_handle_post_request_file_video(mock_server_result, tmpdir): m = mock.MagicMock() files = { "file": FileStorage(open(PATH_TO_TEST_VIDEO, "rb"), content_type='video/mkv') } m.files = files m.form = {"fps": 2, "foo": "bar"} split_frames_folder = os.path.join( tmpdir, "{}_split".format(os.path.basename(PATH_TO_TEST_VIDEO))) os.mkdir(split_frames_folder ) # this folder should be deleted by handle post request with mock.patch("mot.serving.inference.request", m): output = handle_post_request(upload_folder=str(tmpdir)) assert len(output["detected_trash"]) == 2 assert "id" in output["detected_trash"][0] assert "frame_to_box" in output["detected_trash"][1] for frame, box in output["detected_trash"][1]["frame_to_box"].items(): assert isinstance(frame, int) assert isinstance(box, list) assert len(box) == 4 assert output["video_length"] == 6 or output["video_length"] == 7 assert output["fps"] == 2 assert "video_id" in output
def test_Transfer_validate(transf): validator = mock.MagicMock() source = FileStorage(stream=BytesIO(b'Hello World')) transf.validator(validator) transf._validate(source, {}) assert validator.call_args == mock.call(source, {})
def download(self, url): """Download the URL's contents, returning a :class:`FileStorage` instance If the URL cannot be opened, returns `None` """ try: handle = urllib.urlopen(url) content_type = handle.headers.get('content-type', 'application/octet-stream') content_length = handle.headers.get('content-length', -1) headers = handle.headers if 'BAD_CONTENT' in self.app.config: handle = StringIO(handle.read()) for line in handle: for bad in self.app.config['BAD_CONTENT']: if bad in line: raise DownloaderError( 'File contains bad content: %r' % bad) handle.seek(0) store = FileStorage(stream=handle, content_type=content_type, content_length=content_length, headers=headers) return store except IOError: return None
def test_handle_post_request_file_image(mock_server_result, tmpdir): data = np.array([[[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]]]) filename = "test.jpg" filepath = os.path.join(tmpdir, filename) cv2.imwrite(filepath, data) m = mock.MagicMock() files = { "file": FileStorage(open(filepath, "rb"), content_type='image/jpg') } m.files = files upload_folder = os.path.join(tmpdir, "upload") os.mkdir(upload_folder) with open(os.path.join(upload_folder, filename), "w+") as f: f.write( "this file should be deleted by the handle post request to be replaced" " by the image we are uploading.") with mock.patch("mot.serving.inference.request", m): output = handle_post_request(upload_folder=upload_folder) expected_output = { "image": output["image"], "detected_trash": [{ "box": [0.0, 0.0, 0.1, 0.1], "label": "bottles", "score": 0.71 }, { "box": [0.0, 0.0, 0.2, 0.2], "label": "fragments", "score": 0.71 }] } assert output == expected_output
def rsa_generate(): if request.method == 'GET': return render_template('rsa/generate/index.html') rsaKeyPair = RSAKeyPair(int(request.form['p']), int(request.form['q']), int(request.form['e'])) filename = request.form['filename'] public_file = FileStorage(stream=BytesIO( rsaKeyPair.public.to_json().encode()), filename=filename + '.pub') public_file.save('static/' + public_file.filename) private_file = FileStorage(stream=BytesIO( rsaKeyPair.private.to_json().encode()), filename=filename + '.pri') private_file.save('static/' + private_file.filename) return render_template('rsa/generate/result.html')
def profile_json(): """Validate the request of the update of a profile. This method will not operate in any user instance but the authenticated one. If there's nobody authenticated, there's no way to execute it successfuly. """ form = social(ProfileForm, False) if not form.validate_on_submit(): # This field is special, it must be validated before anything. If it # doesn't work, the action must be aborted. if not form.csrf_is_valid: return msg.error(_('Invalid csrf token'), 'InvalidCsrfToken') # Usual validation error return utils.format_csrf_error(form, form.errors, 'ValidationError') # Let's save the authenticated user's meta data mget = form.meta.get try: user = authapi.authenticated_user() except authapi.NobodyHome: return redirect(url_for('index')) # First, the specific ones email = mget('email') redologin = False if user.username == user.email and user.username != email \ and not (user.get_meta('twitteruser') or user.get_meta('facebookuser')): flash(_(u'You changed your email, please relogin.')) redologin = True user.username = email user.name = mget('name') user.email = email # Saving the thumbnail form.meta.pop('avatar') if bool(form.avatar.file): flike = form.avatar.file thumb = utils.thumbnail(flike, (48, 48)) form.meta['avatar'] = Upload.imageset.save( FileStorage(thumb, flike.filename, flike.name), 'thumbs/%s' % user.name[0].lower()) # And then, the meta ones, stored in `UserMeta' for key, val in form.meta.items(): user.set_meta(key, val) # return msg.ok({ # 'data': _('User profile updated successfuly'), # 'csrf': form.csrf.data, # }) flash(_(u'Profile update successful'), 'alert-success') if redologin: authapi.logout() return redirect(url_for('auth.login')) else: return redirect(url_for('.profile'))
def test_Transfer_callable(transf): kwargs = {'filehandle': FileStorage(stream=BytesIO(), filename='test.png'), 'metadata': {}, 'validate': True, 'catch_all_errors': False, 'destination': 'test.png'} with mock.patch('flask_transfer.Transfer.save') as mocked_save: transf(**kwargs) assert mocked_save.called assert mocked_save.call_args == mock.call(**kwargs)
def test_string_path_saving(): source = BytesIO() filehandle = FileStorage(stream=source, filename='test.png') dummy_save = transfer._use_filehandle_to_save('test.png') with mock.patch('werkzeug.FileStorage.save') as mocked_save: dummy_save(filehandle, {'buffer_size': None}) assert mocked_save.call_args == mock.call('test.png', None)
def get_custom_file_as_filestorage(filename): file_path = app.config['UPLOADED_CUSTOM_DEST'] / filename try: data = FileStorage(stream=file_path.open(), filename=file_path.basename()) except (IOError, OSError): data = None return data
def test_nest_Transfer_objs(): Outer = transfer.Transfer() Inner = ReportingTransfer() Outer.postprocessor(Inner) dummy_file = FileStorage(stream=BytesIO(), filename='derp.png') Outer.save(dummy_file, metadata={}, destination=lambda *a, **k: True) assert Inner.verify()
def test_writeable_saving(): destination = BytesIO() filehandle = FileStorage(stream=BytesIO(b'hello world')) dummy_save = transfer._use_filehandle_to_save(destination) with mock.patch('werkzeug.FileStorage.save') as mocked_save: dummy_save(filehandle, {'buffer_size': 1}) assert mocked_save.call_count == 1 assert mocked_save.call_args == mock.call(destination, 1)
def test_Transfer_preprocess(transf): @transf.preprocessor def to_upper(filehandle, meta): filehandle.stream = BytesIO(filehandle.stream.read().lower()) return filehandle source = FileStorage(stream=BytesIO(b'Hello World')) transf._preprocess(source, {}) source.seek(0) assert source.read() == b'hello world'
def handle_img_tags(text, entry_id=None, timestamp=None): """Get image tags from the text. Extract embedded images and save them as attachments""" attachments = [] timestamp = timestamp or datetime.now() try: doc = html.document_fromstring(text) except etree.ParserError: return text, attachments for i, element in enumerate(doc.xpath("//*[@src]")): src = element.attrib['src'].split("?", 1)[0] if src.startswith("data:"): header, data = src[5:].split(",", 1) # TODO: find a safer way filetype, encoding = header.split(";") try: raw_image = decode_base64(data.encode("ascii")) except binascii.Error as e: print("failed to decode image!", e) continue try: # TODO: possible to be more clever about the filename? filename = "inline-{}-{}.{}".format( len(raw_image), i, filetype.split("/")[1].lower()) except IndexError: print("weird filetype!?", filetype) continue file_ = FileStorage(io.BytesIO(raw_image), filename=filename, content_type=filetype) attachment = save_attachment(file_, timestamp, entry_id, embedded=True) # TODO: maybe it would be a better idea to use a URL like # "/attachments/<id>" here, and then just have a redirect # to the real URI? That way we could change the way the files # are stored under the hood without bothering about having # to keep URLs unchanged... src = element.attrib["src"] = url_for("get_attachment", path=attachment.path) if element.getparent().tag == "a": element.getparent().attrib["href"] = src attachments.append(attachment) # throw in a cleanup here since we've already parsed the HTML html_clean(doc) # remove some evil tags content = '\n'.join( (etree .tostring(stree, pretty_print=True, method="xml") .decode("utf-8") .strip()) for stree in doc[0].iterchildren() ) return content, attachments
def return_werkzeug_filestorage(request, filename, maxsize=(320, 240)): extension = request.headers['content-type'].split('/')[-1] if extension not in current_app.config['ALLOWED_EXTENSIONS']: raise UploadNotAllowed("Unsupported file format") new_filename = filename + '.' + extension tempfile = StringIO(buf=request.content) tempfile.name = new_filename filestorage = FileStorage(tempfile, filename=new_filename, content_type=request.headers['content-type']) return filestorage
def ecc_encrypt(): if request.method == 'GET': return render_template('ecc/encrypt/index.html') message = request.files['message'].read() filename = 'encrypted_' + request.files['message'].filename kob = int(request.form['koblitz']) coef = int(request.form['x-coef']) const = int(request.form['const']) modulo = int(request.form['modulo']) if not is_prime(modulo): return "Error: Modulo must be prime" curve = EllipticCurve(coef, const, modulo) base_x = int(request.form['bpoint-x']) base_y = int(request.form['bpoint-y']) bpoint = Point(base_x, base_y, modulo) if not curve.contains(bpoint): return "Error: The base point is not a point in the curve" pubkey = request.files.get('pubkey-file', None) if pubkey == None: pubkey_x = int(request.form['pubkey-x']) pubkey_y = int(request.form['pubkey-y']) pubkey = Point(pubkey_x, pubkey_y, modulo) else: pubkey = pubkey.read().decode() pubkey = Point(0, 0, modulo, pubkey) if not curve.contains(pubkey): return "Error: The public key is not a point in the curve" plaintext = encode(curve, message, kob) start = default_timer() k = randrange(1, modulo) ciphertext = [ curve.add(point, curve.multiply(k, pubkey)) for point in plaintext ] ciphertext.append(curve.multiply(k, bpoint)) end = default_timer() string = ' '.join([point.print() for point in ciphertext]) file = FileStorage(stream=BytesIO(string.encode()), filename=filename) file.seek(0, SEEK_END) size = file.tell() file.seek(0) file.save('static/' + filename) return render_template('ecc/encrypt/result.html', plaintext=message, ciphertext=string, time=end - start, filename=filename, size=size)
def test_jfu_server(self): self._test_view_post('frontend.jfu_server', status='200 OK', data=['{', '}'], form=dict(data_file=FileStorage( StringIO("Hello, world"), filename='C:\\fakepath\\DoesntExist.txt', content_type='text/plain; charset=utf-8'), ), viewopts=dict(item_name='WillBeCreated'), content_types=[ 'application/json', ])
def test_file(self): stream = StringIO.StringIO() stream.write("""\ Content-Type: application/octet-stream Content-Length: 1 X-AppEngine-Upload-Creation: 2010-10-01 05:34:00.000000 """) stream.seek(0) headers = {} headers['Content-Type'] = 'image/png; blob-key=foo' f = FileStorage(stream=stream, headers=headers) self.assertNotEqual(parse_blob_info(f, 'my_field_name'), None)
def test_invalid_CREATION(self): stream = StringIO.StringIO() stream.write("""\ Content-Type: application/octet-stream Content-Length: 1 X-AppEngine-Upload-Creation: XXX """) stream.seek(0) headers = {} headers['Content-Type'] = 'image/png; blob-key=foo' f = FileStorage(stream=stream, headers=headers) self.assertRaises(blobstore.BlobInfoParseError, parse_blob_info,f, 'my_field_name')
def test_api_extensions_custom_fields_file(self): """Test rest api custom file field""" class SampleSchema(ma.Schema): """Sample custom file field schema""" class Meta: strict = True input_file = FileField(allowed_file_extensions=('.pdf', )) file_storage = FileStorage(filename='test.pdf') file_datas = SampleSchema().load({'input_file': file_storage}).data assert file_datas['input_file'] == file_storage # Errors # validation: wrong file extension with pytest.raises(ma.ValidationError): SampleSchema().load( {'input_file': FileStorage(filename='not_valid.zip')}) # validation: wrong value type with pytest.raises(ma.ValidationError): SampleSchema().load({'input_file': 'wrong_type'})
def test_Transfer_validate_raises_with_falsey(transf): source = FileStorage(stream=BytesIO(), filename='test.conf') @transf.validator def bad_validator(fh, m): False with pytest.raises(UploadError) as excinfo: transf.save(source, metadata={}, catch_all_errors=False, destination=lambda *a, **k: None) expected = "{0!r}({1!r}, {2!r}) returned False".format(bad_validator, source, {}) assert str(excinfo.value) == expected
def __resource_update_with_upload(cls, context, filename, resource_name, resource_id): file_path = os.path.join(os.path.dirname(__file__), filename) with open(file_path) as f: file_upload = FileStorage(f) resource_dict = _get_action('resource_update')( context, { 'id': resource_id, 'name': resource_name, 'url_type': 'upload', 'resource_type': 'file.upload', 'upload': file_upload }) return resource_dict
def test_boto3_upload_file_func(): conn = boto3.resource('s3', region_name='us-east-1') conn.create_bucket(Bucket=s3_bucket) file = FileStorage(filename='test.txt', stream=io.BytesIO(b'test'), content_type='text') s3_url, unique_secure_filename = upload_file_to_s3(file) body = conn.Object( s3_bucket, unique_secure_filename).get()['Body'].read().decode("utf-8") assert (body == 'test')
def upload_file(): file = request.files["user_file"] temp_name = file.filename file.save(temp_name) to_convert = AudioSegment.from_file(temp_name) new_name = temp_name.rsplit(".", 1)[0]+".wav" os.remove(temp_name) to_convert.export(new_name, format="wav") converted = open(new_name, "rb") file = FileStorage(converted, content_type="audio/wav") output = upload_file_to_s3(file, app.config["S3_BUCKET"]) return (file.filename)
def test_handle_post_request_file_other(tmpdir): filename = "test.pdf" filepath = os.path.join(tmpdir, filename) with open(filepath, "w") as f: f.write("mock data") m = mock.MagicMock() files = { "file": FileStorage(open(filepath, "rb"), content_type='poulet/pdf') } m.files = files upload_folder = os.path.join(tmpdir, "upload_folder") with pytest.raises(NotImplementedError): with mock.patch("mot.serving.inference.request", m): output = handle_post_request(upload_folder=upload_folder) assert os.path.isdir( upload_folder ) # the upload_folder should be created by handle post request
def return_werkzeug_filestorage(request, filename): extension = request.headers['content-type'].split('/')[-1] if extension not in current_app.config['ALLOWED_EXTENSIONS']: raise UploadNotAllowed("Unsupported file format") new_filename = secure_filename(filename + '.' + extension) try: tempfile = StringIO(buf=request.content) except AttributeError: if hasattr(request.stream, 'getvalue'): tempfile = StringIO(buf=request.stream.getvalue()) elif hasattr(request.stream, 'read'): tempfile = StringIO(buf=request.stream.read()) tempfile.name = new_filename filestorage = FileStorage(tempfile, filename=new_filename, content_type=request.headers['content-type']) return filestorage
def bulkupload(): form = BulkUploadForm() if form.validate_on_submit(): # print(form.file) _, file_extension = os.path.splitext( secure_filename(form.file.data.filename)) temp = ''.join([uuid.uuid4().hex, file_extension]) filename = files.save(FileStorage(form.file.data), name=secure_filename(temp)) file_url = files.url(filename) print(filename) utils.process_bulkupload('C:\\test.xlsx', file_url) return render_template('base.html') return render_template('bulkupload.html', title='New Hire - Bulk Upload', form=form)
def process_image(requestfile, maxsize=(170, 130)): fileext = splitext(requestfile.filename)[1].lower() if fileext not in ['.jpg', '.jpeg', '.png', '.gif']: raise UploadNotAllowed("Unsupported file format") img = Image.open(requestfile) img.load() if img.size[0] > maxsize[0] or img.size[1] > maxsize[1]: img.thumbnail(maxsize, Image.ANTIALIAS) boximg = Image.new('RGBA', maxsize, (255, 255, 255, 0)) boximg.paste(img, ((boximg.size[0] - img.size[0]) / 2, (boximg.size[1] - img.size[1]) / 2)) savefile = StringIO() savefile.name = requestfile.filename boximg.save(savefile) savefile.seek(0) return FileStorage(savefile, filename=requestfile.filename, content_type=requestfile.content_type)
def ecc_decrypt(): if request.method == 'GET': return render_template('ecc/decrypt/index.html') message = request.files['message'].read() kob = int(request.form['koblitz']) coef = int(request.form['x-coef']) const = int(request.form['const']) modulo = int(request.form['modulo']) if not is_prime(modulo): return "Error: Modulo must be prime" curve = EllipticCurve(coef, const, modulo) prikey = request.files.get('prikey-file', None) if prikey == None: prikey = int(request.form['prikey']) else: prikey = int(prikey.read()) raw_ciphertext = message.decode().split(' ') while raw_ciphertext[-1] == '': del raw_ciphertext[-1] ciphertext = [Point(0, 0, modulo, point) for point in raw_ciphertext] start = default_timer() point_neg = curve.multiply(prikey, ciphertext[-1]) del ciphertext[-1] point_neg.negate() plaintext = [curve.add(point, point_neg) for point in ciphertext] end = default_timer() string = decode(plaintext, kob) filename = request.form['filename'] file = FileStorage(stream=BytesIO(string), filename=filename) file.seek(0, SEEK_END) size = file.tell() file.seek(0) file.save('static/' + filename) return render_template('ecc/decrypt/result.html', plaintext=string, ciphertext=message.decode(), time=end - start, filename=filename, size=size)