def cat_picture():
image_name = request.args.get('image_name')
image_name = image_name.replace('..', '')
return send_file(os.path.join(os.getcwd(), image_name))
python类send_file()的实例源码
def cat_picture():
image_name = request.args.get('image_name')
if not '..' in image_name:
return 404
return send_file(os.path.join(os.getcwd(), image_name))
def cat_picture():
image_name = request.args.get('image_name')
if not image_name:
return 404
return send_file(os.path.join(os.getcwd(), image_name))
def api_top_hits():
return send_file(common_filepaths['top-hits-1k'])
def test_send_file_regular(self):
app = flask.Flask(__name__)
with app.test_request_context():
rv = flask.send_file('static/index.html')
self.assert_true(rv.direct_passthrough)
self.assert_equal(rv.mimetype, 'text/html')
with app.open_resource('static/index.html') as f:
rv.direct_passthrough = False
self.assert_equal(rv.data, f.read())
rv.close()
def test_send_file_xsendfile(self):
app = flask.Flask(__name__)
app.use_x_sendfile = True
with app.test_request_context():
rv = flask.send_file('static/index.html')
self.assert_true(rv.direct_passthrough)
self.assert_in('x-sendfile', rv.headers)
self.assert_equal(rv.headers['x-sendfile'],
os.path.join(app.root_path, 'static/index.html'))
self.assert_equal(rv.mimetype, 'text/html')
rv.close()
def test_attachment(self):
app = flask.Flask(__name__)
with catch_warnings() as captured:
with app.test_request_context():
f = open(os.path.join(app.root_path, 'static/index.html'))
rv = flask.send_file(f, as_attachment=True)
value, options = parse_options_header(rv.headers['Content-Disposition'])
self.assert_equal(value, 'attachment')
rv.close()
# mimetypes + etag
self.assert_equal(len(captured), 2)
with app.test_request_context():
self.assert_equal(options['filename'], 'index.html')
rv = flask.send_file('static/index.html', as_attachment=True)
value, options = parse_options_header(rv.headers['Content-Disposition'])
self.assert_equal(value, 'attachment')
self.assert_equal(options['filename'], 'index.html')
rv.close()
with app.test_request_context():
rv = flask.send_file(StringIO('Test'), as_attachment=True,
attachment_filename='index.txt',
add_etags=False)
self.assert_equal(rv.mimetype, 'text/plain')
value, options = parse_options_header(rv.headers['Content-Disposition'])
self.assert_equal(value, 'attachment')
self.assert_equal(options['filename'], 'index.txt')
rv.close()
def test_static_file(self):
app = flask.Flask(__name__)
# default cache timeout is 12 hours
with app.test_request_context():
# Test with static file handler.
rv = app.send_static_file('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 12 * 60 * 60)
rv.close()
# Test again with direct use of send_file utility.
rv = flask.send_file('static/index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 12 * 60 * 60)
rv.close()
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600
with app.test_request_context():
# Test with static file handler.
rv = app.send_static_file('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 3600)
rv.close()
# Test again with direct use of send_file utility.
rv = flask.send_file('static/index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 3600)
rv.close()
class StaticFileApp(flask.Flask):
def get_send_file_max_age(self, filename):
return 10
app = StaticFileApp(__name__)
with app.test_request_context():
# Test with static file handler.
rv = app.send_static_file('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 10)
rv.close()
# Test again with direct use of send_file utility.
rv = flask.send_file('static/index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 10)
rv.close()
def api_family_export_sampleszip(family_id, tlp_level):
my_family = api.get_elem_by_type("family", family_id)
zpath = api.familycontrol.generate_samples_zip_file(my_family, tlp_level)
if zpath is None:
return ""
return send_file("../" + zpath, as_attachment=True,
attachment_filename="export.tar.gz")
def download_family_file(family_id, file_id):
"""
Family attachment download endpoint.
"""
attachment = api.get_elem_by_type("family_file", file_id)
data_file = attachment.filepath
if not os.path.exists(data_file):
abort(404)
return send_file('../' + data_file,
as_attachment=True,
attachment_filename=os.path.basename(data_file))
def api_get_sample_file(sid):
"""
Return the sample binary
"""
sample = api.get_elem_by_type("sample", sid)
data_file = sample.storage_file
return send_file('../' + data_file,
as_attachment=True,
attachment_filename=os.path.basename(data_file))
def cache(filename):
if not config.CACHE_IMAGES:
return g.plex.get_thumb_data(filename)
cache_dir = os.path.join(config.DATA_DIR, "cache")
cache_file = os.path.join(cache_dir, filename)
if not os.path.exists(cache_file + ".jpg"):
if helper.cache_file(filename, g.plex):
return send_from_directory(cache_dir, filename + ".jpg")
else:
return send_file('static/images/poster.png')
else:
return send_from_directory(cache_dir, filename + ".jpg")
def image_jpg():
myCamera.getImage().save('/home/pi/boot/image.jpg')
return send_file('/home/pi/boot/image.jpg', attachment_filename='image.jpg')
def home():
return send_file(os.path.realpath(os.path.join(app.static_folder, 'index.html')))
def font():
return send_file(os.path.realpath(os.path.join('SFPixelate-Bold.ttf')))
#return send_from_directory('/', 'SFPixelate-Bold.ttf')
def index():
return send_file('templates/index.html')
def get_vxstream_download(sha256, eid, ftype):
Sample.query.filter_by(sha256=sha256).first_or_404()
headers = {
'Accept': 'text/html',
'User-Agent': 'VxStream Sandbox API Client'}
params = {'type': ftype, 'environmentId': eid}
vx = vxstream.api.get('result/{}'.format(sha256),
params=params, headers=headers)
if ftype in ['xml', 'html', 'bin', 'pcap']:
ftype += '.gz'
return send_file(BytesIO(vx),
attachment_filename='{}.{}'.format(sha256, ftype),
as_attachment=True)
def get_fireeye_download(sha256, eid, ftype):
raise ApiException({}, 501)
Sample.query.filter_by(sha256=sha256).first_or_404()
headers = {
'Accept': 'text/html',
'User-Agent': 'FireEye Sandbox API Client'}
params = {'type': ftype, 'environmentId': eid}
vx = fireeye.api.get('result/{}'.format(sha256),
params=params, headers=headers)
if ftype in ['xml', 'html', 'bin', 'pcap']:
ftype += '.gz'
return send_file(BytesIO(vx),
attachment_filename='{}.{}'.format(sha256, ftype),
as_attachment=True)
def download_file(file_id):
"""Download file
**Example request**:
.. sourcecode:: http
GET /api/1.0/files/1 HTTP/1.1
Host: do.cert.europa.eu
Accept: application/json
**Example response**:
.. sourcecode:: http
HTTP/1.0 200 OK
Content-Type: application/json
Content-Disposition: attachment; filename=CIMBL-244-EU.zip
Content-Length: 55277
Content-Type: application/zip
:param file_id: file's unique ID
:reqheader Accept: Content type(s) accepted by the client
:resheader Content-Type: this depends on `Accept` header or request
:status 200: File found
:status 404: Resource not found
"""
dfile = DeliverableFile.query.filter_by(id=file_id).first_or_404()
cfg = current_app.config
return send_file(os.path.join(cfg['APP_UPLOADS'], dfile.name),
attachment_filename=dfile.name, as_attachment=True)
def get_cp_vxstream_download(sha256, eid, ftype):
Sample.query.filter_by(sha256=sha256, user_id=g.user.id).first_or_404()
headers = {
'Accept': 'text/html',
'User-Agent': 'VxStream Sandbox API Client'}
params = {'type': ftype, 'environmentId': eid}
vx = vxstream.api.get('result/{}'.format(sha256),
params=params, headers=headers)
if ftype in ['xml', 'html', 'bin', 'pcap']:
ftype += '.gz'
return send_file(BytesIO(vx),
attachment_filename='{}.{}'.format(sha256, ftype),
as_attachment=True)