|
|
@ -19,7 +19,7 @@ from mastodon import Mastodon |
|
|
|
from datetime import date, datetime |
|
|
|
from functools import wraps |
|
|
|
import hashlib |
|
|
|
from zipfile import ZipFile |
|
|
|
import shutil |
|
|
|
import random |
|
|
|
import os |
|
|
|
import re |
|
|
@ -204,15 +204,17 @@ def upload(username): |
|
|
|
abort(422) |
|
|
|
|
|
|
|
files = request.files.getlist('files[]') |
|
|
|
print(files) |
|
|
|
|
|
|
|
dir_name = username + str(datetime.now()) |
|
|
|
base_path = os.path.join('/tmp', dir_name) |
|
|
|
os.mkdir(base_path) |
|
|
|
for f in files: |
|
|
|
filename = f.filename.replace('/','_') |
|
|
|
filename = f.filename.replace('..','__') |
|
|
|
os.makedirs(os.path.dirname(os.path.join(base_path, filename)), exist_ok=True) |
|
|
|
f.save(os.path.join(base_path, filename)) |
|
|
|
|
|
|
|
res = ipfs_client.add(base_path) |
|
|
|
res = ipfs_client.add(base_path, recursive=True) |
|
|
|
file_hash = '' |
|
|
|
for r in res: |
|
|
|
if r.get('Name') == dir_name: |
|
|
@ -250,17 +252,25 @@ 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) |
|
|
|
dformat = request.args.get('format') |
|
|
|
suff = { |
|
|
|
'zip': '.zip', |
|
|
|
'tar': '.tar', |
|
|
|
'gztar': '.tar.gz' |
|
|
|
} |
|
|
|
|
|
|
|
if dformat in suff: |
|
|
|
target_file = '/tmp/%s' % pid |
|
|
|
target_filename = target_file + suff[dformat] |
|
|
|
target_dir = os.path.join('/tmp', p.file_hash) |
|
|
|
|
|
|
|
if not os.path.exists(target_filename): |
|
|
|
if not os.path.exists(target_dir): |
|
|
|
ipfs_client.get(p.file_hash, target='/tmp') |
|
|
|
shutil.make_archive(target_file, dformat, target_dir) |
|
|
|
|
|
|
|
filename = re.sub('[^\w@_()()-]', '_', '%s_%s_共享计划_%d' %(p.course, p.teacher, p.id)) + suff[dformat] |
|
|
|
return send_file(target_filename, as_attachment=True, attachment_filename=filename) |
|
|
|
|
|
|
|
return redirect(C.ipfs_base_url + p.file_hash, code=301) # 301减少不必要的请求 |
|
|
|
|
|
|
|