-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
178 lines (125 loc) · 4.81 KB
/
main.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
166
167
168
169
170
171
172
173
174
175
176
177
178
from flask import Flask, flash, request, redirect, send_file, render_template, url_for
import os
from werkzeug.utils import secure_filename
import shutil
# Defining constants
IPv4 = "your_IPv4"
PORT = 1111
UPLOAD_FOLDER = 'your_path/Cloud_storage/static/server/'
FOLDER = os.listdir(UPLOAD_FOLDER)
LOWER_FOLDER = len(UPLOAD_FOLDER.split("/"))
WORKSPACE = UPLOAD_FOLDER.split("/")[-2] + "/"
# Starting flask app
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.static_folder = 'static'
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
# Uploading files
@ app.route('/', methods=['GET', 'POST'])
def files():
FOLDER = os.listdir(app.config['UPLOAD_FOLDER'])
p = app.config['UPLOAD_FOLDER'].rsplit('/', 2)
flash(p[1])
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
return redirect(request.url)
else:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
FOLDER.append(filename)
return render_template('file.html', value=FOLDER, only_folder="False", file_to_move=None, actual_path=app.config['UPLOAD_FOLDER'].replace("/", " "))
# Downloading files
@ app.route('/return-files/<filename>', methods=['GET', 'POST'])
def download_file(filename):
file_path = app.config['UPLOAD_FOLDER'] + filename
return send_file(file_path, as_attachment=True, attachment_filename='')
# Going through the folders
@ app.route('/directory/<folder>/<only_folder>/<file_to_move>', methods=['GET', 'POST'])
def directory(folder, only_folder, file_to_move):
if folder not in app.config['UPLOAD_FOLDER']:
app.config['UPLOAD_FOLDER'] = app.config['UPLOAD_FOLDER'] + folder + "/"
else:
return redirect("/")
if only_folder == "True":
return redirect(f"/move-mode/{file_to_move}")
return redirect("/")
# Going back in folders
@ app.route('/back/<only_folder>/<file_to_move>', methods=['GET', 'POST'])
def back(only_folder, file_to_move):
rout = app.config['UPLOAD_FOLDER'].split('/')
length = len(rout)
if length > LOWER_FOLDER:
route = app.config['UPLOAD_FOLDER'].rsplit('/', 2)
r = route[0] + '/'
else:
r = app.config['UPLOAD_FOLDER']
app.config['UPLOAD_FOLDER'] = r
if only_folder == "True":
return redirect(f"/move-mode/{file_to_move}")
return redirect("/")
# Creating a folders
@ app.route('/new-folder', methods=['GET', 'POST'])
def new_folder():
if request.method == 'POST':
if 'folder' not in request.form:
return redirect("/")
else:
folder_name = request.form.get('folder')
path = os.path.join(app.config['UPLOAD_FOLDER'], folder_name)
try:
os.mkdir(path)
except Exception:
flash("Write the name")
return redirect("/")
return redirect("/")
# Deleting files and empty folders
@ app.route('/delete/<file>', methods=['GET', 'POST'])
def delete(file):
try:
if "." in file:
os.remove(app.config['UPLOAD_FOLDER'] + file)
else:
os.rmdir(app.config['UPLOAD_FOLDER'] + file + "/")
except Exception:
flash("The directory is not empty")
return redirect("/")
# Moving mode
@ app.route('/move-mode/<file>', methods=['GET', 'POST'])
def move_mode(file):
FOLDER = os.listdir(app.config['UPLOAD_FOLDER'])
p = app.config['UPLOAD_FOLDER'].rsplit('/', 2)
flash(p[1])
return render_template('file.html', value=FOLDER, only_folder="True", file_to_move=file, actual_path=app.config['UPLOAD_FOLDER'].replace("/", " "))
# Moving files
@ app.route('/move/<file>', methods=['GET', 'POST'])
def move(file):
start_pos = file.replace(" ", "/")
try:
shutil.move(start_pos, app.config["UPLOAD_FOLDER"])
except Exception:
flash("Destination path already exists")
return redirect("/")
# Preview
@ app.route('/preview/<file>', methods=['GET', 'POST'])
def preview(file):
path = app.config['UPLOAD_FOLDER']
if ".txt" in file:
with open(path + file, 'r', encoding="utf-8") as file:
file = file.read().replace('\n', '')
elif ".png" in file or ".jpg" in file or ".jpeg" in file or ".jfif" in file or ".gif" in file or ".ico" in file or ".svg" in file or ".mp4" in file:
path = path.replace(UPLOAD_FOLDER, "") + WORKSPACE
file = path + file
else:
pass
return render_template('preview.html', file=file)
# Restarting main route -> app.config['UPLOAD_FOLDER']
@ app.route("/restart")
def restart():
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
return redirect("/")
# Run flask app
if __name__ == "__main__":
app.run(debug=True, host=IPv4, port=PORT)