|
|
@ -1,4 +1,4 @@ |
|
|
|
from flask import Flask, request, render_template, send_from_directory, abort, redirect, session, url_for |
|
|
|
from flask import Flask, request, render_template, send_from_directory, abort, redirect, session, url_for, send_file |
|
|
|
from flask_sqlalchemy import SQLAlchemy |
|
|
|
from flask_limiter import Limiter |
|
|
|
from flask_limiter.util import get_remote_address |
|
|
@ -9,8 +9,10 @@ from mastodon import Mastodon |
|
|
|
from datetime import date, datetime |
|
|
|
from functools import wraps |
|
|
|
import hashlib |
|
|
|
from zipfile import ZipFile |
|
|
|
import random |
|
|
|
import os |
|
|
|
import re |
|
|
|
from config import C |
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
@ -47,6 +49,9 @@ class Paper(db.Model): |
|
|
|
down_num = db.Column(db.Integer, index=True, default=0) |
|
|
|
file_hash = db.Column(db.String(64)) |
|
|
|
|
|
|
|
# always increment 1 for the id of a new record |
|
|
|
__table_args__ = { 'sqlite_autoincrement': True } |
|
|
|
|
|
|
|
def is_downloaded(self): |
|
|
|
return bool(DownloadRelation.query.filter_by(paper_id=self.id, username=session.get('username')).count()) |
|
|
|
|
|
|
@ -216,6 +221,10 @@ def upload(username): |
|
|
|
|
|
|
|
return redirect('.#part2') |
|
|
|
|
|
|
|
@app.route('/pastExam/test') |
|
|
|
def test_download(): |
|
|
|
return send_file('/tmp/id.zip', as_attachment=True, attachment_filename='name.zip') |
|
|
|
|
|
|
|
@app.route('/pastExam/<pid>/download') |
|
|
|
@login_required() |
|
|
|
def download(pid, username): |
|
|
@ -229,6 +238,18 @@ def download(pid, username): |
|
|
|
p.down_num += 1 |
|
|
|
db.session.commit() |
|
|
|
|
|
|
|
if request.args.get('type') == 'zip': |
|
|
|
target_file = '/tmp/%s.zip' % pid |
|
|
|
|
|
|
|
if not os.path.exists(target_file): |
|
|
|
ipfs_client.get(p.file_hash, target='/tmp') |
|
|
|
with ZipFile(target_file, 'w') as z: |
|
|
|
for fname in os.listdir(os.path.join('/tmp', p.file_hash)): |
|
|
|
z.write(os.path.join('/tmp', p.file_hash, fname), fname) |
|
|
|
|
|
|
|
filename = re.sub('[^\w@_()()-]', '_', '%s_%s_共享计划_%d' %(p.course, p.teacher, p.id)) + '.zip' |
|
|
|
return send_file(target_file, as_attachment=True, attachment_filename=filename) |
|
|
|
|
|
|
|
return redirect(C.ipfs_base_url + p.file_hash, code=301) # 301减少不必要的请求 |
|
|
|
|
|
|
|
@app.route('/pastExam/<pid>/like', methods=['POST', 'DELETE']) |
|
|
|