Skip to content

Instantly share code, notes, and snippets.

@aaronjolson
Last active October 10, 2018 23:42
Show Gist options
  • Save aaronjolson/55461ef25f50f6f06e7bdc0089126f72 to your computer and use it in GitHub Desktop.
Save aaronjolson/55461ef25f50f6f06e7bdc0089126f72 to your computer and use it in GitHub Desktop.
from flask import Flask
from flask import make_response
import os
app = Flask(__name__)
# Simple flask server to be used locally for serving images to get around constraints such as those created
# when trying to load textures in ThreeJs.
# https://stackoverflow.com/questions/24087757/three-js-and-loading-a-cross-domain-image
# Windows Running instuctions, assumes Python and Flask are installed,
# from inside the directory where this file is located run
# set FLASK_APP=flask_static_server.py
# flask run
# Once running, images can be reached at, for example, http://127.0.0.1:5000/getimages/noise.png
# Directory where the images to be retrieved are stored, using windows pathing
dataDir = os.path.expanduser(r'~\Downloads\static')
@app.route('/getfile/<filename>')
def getFile(filename):
filename = dataDir + '\\' + filename
try:
with open(filename, "rb") as file:
blob = file.read()
except:
return 'The requested file could not be found'
r = make_response(blob)
# rough check to determine the file extension in order to set an exact mimetype.
if filename[-3:] == 'jpg':
r.mimetype='image/jpg'
elif filename[-4:] == 'jpeg':
r.mimetype='image/jpeg'
elif filename[-3:] == 'gif':
r.mimetype='image/gif'
else:
r.mimetype='image/png'
r.headers['Access-Control-Allow-Origin'] = '*'
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment