diff --git a/app.py b/app.py index 0aa807c..2a8de04 100644 --- a/app.py +++ b/app.py @@ -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减少不必要的请求 diff --git a/templates/list.html b/templates/list.html index 3481eea..2fa8d57 100644 --- a/templates/list.html +++ b/templates/list.html @@ -229,6 +229,10 @@ .wechat-share img { width: 196px; } + + .custom-file-label { + overflow: hidden; + } @@ -296,6 +300,10 @@ +
+ + +
@@ -313,7 +321,7 @@ {% endif %} -
+
@@ -503,7 +511,21 @@ } $(document).ready(function () { - bsCustomFileInput.init() + bsCustomFileInput.init(); + $("#upload-dir").change(function() { + var fi = $('.custom-file-input'); + var fl = $('.custom-file-label'); + console.log(fi); + if(this.checked) { + fi.attr('webkitdirectory', true); + fl.text('选择文件夹,可以多选'); + } + else { + fi.removeAttr('webkitdirectory'); + fl.text('选择文件,可以多选'); + } + }); + $('[data-toggle="popover"]').popover({ html: true, placement: "top",