-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
136 lines (97 loc) · 3.95 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os
import argparse
from utils import *
from pathlib import Path
from flask import Flask, render_template, Response, request, redirect, url_for, make_response, jsonify
app = Flask(__name__)
session = {}
images_per_scroll = 16
@app.route('/get_fullsize_image', methods=['POST'])
def get_fullsize_image():
json = request.get_json()
image_path = json.get('path')
image_bytes, size = open_image(image_path, resize=False)
if not image_bytes:
return make_response(jsonify(f'Error while opening image, {image_path}'), 404)
image_data = f"data:image/png;base64,{b64encode(image_bytes).decode('utf-8')}"
return make_response(jsonify(image_data), 200)
@app.route('/load')
def load():
if not request.args:
return make_response(jsonify("No arguments"), 400)
counter = int(request.args.get("counter"))
print(
f'Loading images from {counter} to {counter + images_per_scroll}')
file_names = session.get('file_names')[
counter: counter + images_per_scroll]
res = make_response(
jsonify(get_images_data(session.get('start_directory'), file_names)), 200)
return res
@app.route('/move', methods=['POST'])
def move_images():
current_path = session.get('start_directory')
json = request.get_json()
destination_path = json.get('destination')
checkbox_values = json.get('items')
print('Checkbox values', checkbox_values)
if not os.path.isdir(destination_path):
os.makedirs(destination_path)
print(f'Creating {destination_path} directory')
moved_counter = 0
for image_path in checkbox_values:
image_name = os.path.basename(image_path)
new_path = os.path.join(destination_path, image_name)
print(f'Moving {image_name} to {destination_path}')
try:
Path(image_path).rename(new_path)
except Exception as e:
print(image_path, e)
continue
moved_counter += 1
session['file_names'] = get_files_list(current_path)
return make_response(jsonify(f'{moved_counter} image(s) successfully moved to {destination_path}'), 200)
@app.route('/get_path_options', methods=['GET'])
def get_path_options():
directory_path = request.args.get('path')
path_options = path_completer(directory_path)
return make_response(jsonify(path_options), 200)
@app.route('/enter', methods=['GET'])
def get_directory_path():
directory_path = request.args.get('directory')
if not directory_path or not os.path.isdir(directory_path):
print('Cannot find such directory')
return make_response(jsonify('Cannot find such directory'), 400)
session['start_directory'] = directory_path
session['file_names'] = get_files_list(directory_path)
return redirect('/directory_view')
@app.route('/directory_view')
def main_view():
total = 0
if session.get('file_names'):
total = len(session.get('file_names'))
return render_template('directory.html', current_directory=session.get('start_directory'),
images_per_scroll=images_per_scroll, total=total)
@app.route('/')
def index():
ip = request.remote_addr
if not session.get(ip):
return render_template('login.html', message=session.get('message'))
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
ip = request.remote_addr
password = request.form.get('password')
if password != app.secret_key:
session['message'] = 'Wrong code, try again'
return redirect('/')
session[ip] = password
return redirect('/')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--port', default=8080, type=int)
args = parser.parse_args()
port_number = args.port
app.secret_key = generate_session_password()
print('Current session code:', app.secret_key)
# debug is set to true to avoid jinja templates caching
app.run(debug=True, host='0.0.0.0', port=port_number)