From cbdd3dfe3dabd568e8a8bf3af469c47699864f22 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Olivier=20T=C3=A9tard?= Date: Fri, 13 Nov 2015 11:00:57 +0100 Subject: [PATCH] =?utf8?q?Si=20un=20fichier=20existe=20d=C3=A9j=C3=A0,=20n?= =?utf8?q?e=20pas=20l=E2=80=99=C3=A9craser=20mais=20en=20cr=C3=A9er=20un?= =?utf8?q?=20nouveau?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- femtoblackweb.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/femtoblackweb.py b/femtoblackweb.py index c581705..aafd48d 100644 --- a/femtoblackweb.py +++ b/femtoblackweb.py @@ -51,16 +51,27 @@ def upload_file(): if request.method == 'POST': f = request.files['file'] - result = { - 'name': f.filename, - 'type': f.mimetype, - } - + result = {} + if allowed_file(f.filename): filename = secure_filename(f.filename) + (basename, ext) = os.path.splitext(filename) + dst_file = os.path.join(app.config['UPLOAD_FOLDER'], filename) + + # Si le fichier existe déjà, on rajoute un compteur avant + # l’extension. + n = 0 + while os.path.isfile(dst_file): + n += 1 + filename = "{} ({}){}".format(basename, n, ext) + dst_file = os.path.join(app.config['UPLOAD_FOLDER'], filename) + f.save(dst_file) result['size'] = os.path.getsize(dst_file) + result['name'] = filename + result['type'] = f.mimetype + else: result['size'] = 0 result['error'] = 'Filetype not allowed' @@ -73,12 +84,12 @@ def upload_file(): @app.route('/download/') def download(filename): return send_from_directory(app.config['UPLOAD_FOLDER'], filename) - + @app.route('/analyze/') def analyze_file(filename): f = os.path.join(app.config['UPLOAD_FOLDER'], filename) - if os.path.exists(f): + if os.path.exists(f): return render_template('analyze_file.html', filename=filename) else: @@ -158,7 +169,7 @@ def test_disconnect(): - + if __name__ == "__main__": socketio.run(app) #app.run(debug=True) -- 2.30.2