1.修复
This commit is contained in:
parent
3bb2c6862d
commit
724b3ed354
|
|
@ -1,10 +1,12 @@
|
|||
from flask import Flask
|
||||
from flask import request
|
||||
from flask import request,send_file
|
||||
from flask_cors import CORS
|
||||
import util
|
||||
import db.model
|
||||
from db.model import Pic
|
||||
from db.connector import Session
|
||||
import config
|
||||
import os.path
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
|
@ -15,19 +17,32 @@ def upload():
|
|||
# app.logger.debug("Got upload post.")
|
||||
if request.method != "POST":
|
||||
return "{status:'Err',msg:'only post is supported.'}"
|
||||
pic_storage_dir='/home/dmy/doc/pic_storage'
|
||||
f=request.files['up']
|
||||
file_binary=f.read()
|
||||
md5=util.md5(file_binary)
|
||||
#先检查是否已经有图片了
|
||||
session=Session()
|
||||
# test_existed_pic=Pic(md5=md5)
|
||||
if session.query(Pic.md5).filter(Pic.md5==md5).first() is not None:
|
||||
app.logger.debug('pic with md5 {md5} esisted.'.format(md5=md5))
|
||||
return '{ status:"succeed" }'
|
||||
save_path='{pic_storage_dir}/{md5}'.format(pic_storage_dir=pic_storage_dir,md5=md5)
|
||||
utc_now=util.utc_now()
|
||||
db.model.add_pic(md5=md5,path=save_path,time=utc_now)
|
||||
f.save(save_path)
|
||||
pic_storage_dir = config.get("GENERAL", "Picture_Storage_Location")
|
||||
f = request.files["up"]
|
||||
file_binary = f.read()
|
||||
md5 = util.md5(file_binary)
|
||||
# 先检查是否已经有图片了
|
||||
session = Session()
|
||||
if session.query(Pic.md5).filter(Pic.md5 == md5).first() is not None:
|
||||
app.logger.debug("pic with md5 {md5} esisted.".format(md5=md5))
|
||||
# return '{ status:"succeed" }'
|
||||
save_path = "{pic_storage_dir}/{md5}".format(
|
||||
pic_storage_dir=pic_storage_dir, md5=md5
|
||||
)
|
||||
utc_now = util.utc_now()
|
||||
db.model.add_pic(md5=md5, path=save_path, time=utc_now)
|
||||
with open(save_path,'bw') as f_img:
|
||||
f_img.write(file_binary)
|
||||
return '{ status:"succeed" }'
|
||||
|
||||
|
||||
@app.route("/img/<string:img_md5>")
|
||||
def img(img_md5):
|
||||
pic_location_dir = config.get("GENERAL", "Picture_Storage_Location")
|
||||
img_path = "{dir}/{file}".format(dir=pic_location_dir, file=img_md5)
|
||||
if os.path.exists(img_path):
|
||||
app.logger.debug('Visited imge {img_md5}'.format(img_md5=img_md5))
|
||||
return send_file(img_path,mimetype='image/jpeg')
|
||||
app.logger.debug('Non-existend image {img_md5} visited'.format(img_md5=img_md5))
|
||||
return ""
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue