-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
165 lines (153 loc) · 6.02 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from flask import Flask, Response, render_template, send_file, jsonify, request, redirect, url_for
import os
# from pdf2image import convert_from_bytes
# from PyPDF2 import PdfWriter, PdfReader
import io
import base64
import cv2
# flask variables
app = Flask(__name__)
# PATH = r"C:/ThefCraft/"
PATH = r'demoFolder'
icon_mapping = {
'ai': 'adobe-illustrator.png',
'apk': 'apk.png',
'css': 'css.png',
'disc': 'disc.png',
'doc': 'word.png',
'docx': 'word.png',
'eot': 'font-file.png', # Change this to 'ttf.png' or 'otf.png' if desired
'excel': 'excel.png',
'flac': 'music.png', # Assuming this is a music file extension
'gif': 'image.png', # Assuming this is an image file extension
'html': 'text.png', # Assuming this is a text file extension
'image': 'image.png',
'iso': 'iso.png',
'java': 'js-file.png', # Change this to 'javascript.png' if desired
'javascript': 'js-file.png', # Change this to 'javascript.png' if desired
'jpeg': 'image.png', # Assuming this is an image file extension
'jpg': 'image.png', # Assuming this is an image file extension
'js': 'js-file.png', # Change this to 'javascript.png' if desired
'jsx': 'js-file.png', # Change this to 'javascript.png' if desired
'mail': 'mail.png',
'mkv': 'video.png', # Assuming this is a video file extension
'mp3': 'music.png',
'mp4': 'video.png', # Assuming this is a video file extension
'mpeg': 'video.png', # Assuming this is a video file extension
'mpg': 'video.png', # Assuming this is a video file extension
'music': 'music.png',
'ogg': 'music.png', # Assuming this is a music file extension
'pdf': 'pdf.png',
'php': 'php.png',
'png': 'image.png', # Assuming this is an image file extension
'powerpoint': 'powerpoint.png',
'ppt': 'powerpoint.png',
'psd': 'psd.png',
'record': 'record.png',
'sql': 'sql.png',
'svg': 'svg.png',
'text': 'text.png',
'ttf': 'ttf.png', # Change this to 'font-file.png' or 'otf.png' if desired
'txt': 'text.png',
'vector': 'vector.png',
'vob': 'video.png', # Assuming this is a video file extension
'wav': 'music.png', # Assuming this is a music file extension
'webm': 'video.png', # Assuming this is a video file extension
'webp': 'image.png', # Assuming this is an image file extension
'word': 'word.png',
'xls': 'excel.png',
'xlsx': 'excel.png',
'xml': 'text.png', # Assuming this is a text file extension
'zip': 'zip.png',
}
def getIcon(path:str):
ext = os.path.splitext(path)[1].removeprefix('.')
return icon_mapping.get(ext, "javascript.png")
def getGridViewIcon(path:str):
ext = os.path.splitext(path)[1].removeprefix('.')
if ext in ['png', 'jpg', 'jpeg']:
return f'files/{path}'
elif ext in ['mp4', 'webm', 'vob', 'mpg', 'mpeg', 'mkv']:
return f'api/getImageFromVideo/{path}' # return f'data:image/png;base64, {base64.b64encode(thumb_buf.tobytes()).decode()}'
CHUNK_SIZE = 128
# TEMP_STORAGE = {}
def preProcesser(path, chunk_idx=0):
result = []
client_path = path
path = os.path.join(PATH, path)
if not os.path.exists(path): return result
# files = TEMP_STORAGE.get(path, None)
# if files:
# print('loading files from %s via chunk so if any changes not visable' % path)
# else:
# files = os.listdir(path)
# TEMP_STORAGE.update({
# path: files
# })
files = os.listdir(path)
for file in files[chunk_idx*CHUNK_SIZE:chunk_idx*CHUNK_SIZE+CHUNK_SIZE]:
fullPath = os.path.join(path, file)
result.append((os.path.join(client_path, file), os.path.isdir(fullPath), file, getIcon(fullPath), getGridViewIcon(fullPath)))
return sorted(result, key=lambda x: x[1], reverse=True)
def getNavigation(path):
result = [(path, os.path.split(path)[1])]
while True:
path = os.path.split(path)[0]
if path == '': break
result.append((path, os.path.split(path)[1]))
return [('', 'root')]+result[::-1]
@app.route('/api/upload', methods=['POST'])
def upload():
for fpathuser, fobj in request.files.items():
fpath = fpathuser.split('|files_')[0]
fobj.save(os.path.join(os.path.join(PATH, fpath), fobj.filename))
print('UPLOADING', fobj.filename, 'TO', os.path.join(os.path.join(PATH, fpath), fobj.filename))
return 'OK'
@app.route('/api/getImageFromVideo/<path:path>')
def getThumbnailFromVideo(path):
threshold = 10
vcap = cv2.VideoCapture(path)
res, im_ar = vcap.read()
# while im_ar.mean() < threshold and res: res, im_ar = vcap.read()
res, thumb_buf = cv2.imencode('.png', im_ar)
bte = thumb_buf.tobytes()
vcap.release()
return bte
@app.route('/api/load', methods=['POST'])
def load():
idx = request.json.get("idx")
path = request.json.get("path")
print(path, idx)
return jsonify({
'files':preProcesser(path, chunk_idx=idx)
})
def httpredirect(url):
return f'''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0; URL='{url}'">
<title>Redirecting...</title>
</head>
<body>
<p>Redirecting to {url}...</p>
</body>
</html>'''
@app.route('/')
def reload():
return httpredirect('/files/')
# return redirect('/files/')
@app.route('/files/', defaults={'path': ''})
@app.route('/files/<path:path>')
def get_dir(path):
if not os.path.exists(path) and path == 'favicon.ico': return send_file('./static/icon.svg')
if not os.path.exists(path) and path == 'favicon.ico': return send_file('./static/icon.svg')
if not os.path.realpath(os.path.join(PATH, path)).startswith(os.path.realpath(PATH)):
return httpredirect('/files/')
if os.path.isdir(os.path.join(PATH, path)):
return render_template('index.html', files=preProcesser(path), navigation=getNavigation(path))
else:
return send_file(os.path.join(PATH, path))
if __name__ == '__main__':
# app.run()
app.run(debug=False, host='0.0.0.0', port=80)#, ssl_context=('cert.pem', 'key.pem'))