Se dejo mas bonito, con una vista de preview donde puedo editar los nombres de los archivos o eliminarlos ademas de una vista de about, donde alguien puede contactarme en caso de haber algun problema con sus imagenes

This commit is contained in:
Daniel Cortes
2019-02-20 03:42:27 -03:00
parent 6e9af8302d
commit 9f7d1b668b
10 changed files with 177 additions and 6 deletions

View File

@@ -1,16 +1,25 @@
import os
import random
from flask import Flask, Blueprint, flash, request, redirect, url_for, current_app, render_template, send_from_directory
from werkzeug.utils import secure_filename
from werkzeug.exceptions import abort
from files.auth import admin_required
import random
bp = Blueprint('files', __name__)
bp.add_url_rule('/uploads/<path:filename>', 'uploaded_file', build_only=True)
def get_extension(filename):
return filename.rsplit('.', 1)[1].lower()
def get_path_in_upload(filename):
return os.path.join(current_app.config['UPLOAD_FOLDER'], filename)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in current_app.config['ALLOWED_EXTENSIONS']
return '.' in filename and get_extension(filename) in current_app.config['ALLOWED_EXTENSIONS']
@bp.route('/', methods=['GET', 'POST'])
def index():
@@ -30,8 +39,45 @@ def upload_file():
return rediret(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename))
file.save(get_path_in_upload(filename))
return redirect(url_for('index'))
return redirect(url_for('files.preview_file', filename=filename))
return render_template('files/upload.html')
@bp.route('/preview/<path:filename>')
def preview_file(filename):
file = os.path.isfile(get_path_in_upload(filename))
if os.path.isfile(get_path_in_upload(filename)):
return render_template('files/preview.html', filename=filename)
else:
abort(404)
@bp.route('/rename/<path:filename>', methods=['POST'])
@admin_required
def rename_file(filename):
if request.method == 'POST' and os.path.isfile(get_path_in_upload(filename)):
new_name = request.form['new_name'].lower()
extension = filename.rsplit('.', 1)[1].lower()
if "." in new_name and get_extension(new_name):
new_name = new_name.rsplit('.',1)[0] + '.' + extension
else:
new_name = new_name + '.' + extension
new_name = secure_filename(new_name)
os.rename(get_path_in_upload(filename), get_path_in_upload(new_name))
return redirect(url_for('files.preview_file', filename=new_name))
@bp.route('/delete/<path:filename>', methods=['POST'])
@admin_required
def delete_file(filename):
full_path = os.path.join(current_app.config['UPLOAD_FOLDER'], filename)
if request.method == 'POST' and os.path.isfile(full_path):
os.remove(full_path)
return redirect(url_for('index'))
else:
abort(404)