-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.py
103 lines (75 loc) · 2.85 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
import os
import time
import glob
from flask import Flask, redirect, render_template, request, send_file
# Configure Application
app = Flask(__name__)
global filename
global ftype
@app.route("/")
def home():
# Delete old files
filelist = glob.glob('uploads/*')
for f in filelist:
os.remove(f)
filelist = glob.glob('downloads/*')
for f in filelist:
os.remove(f)
return render_template("home.html")
app.config["FILE_UPLOADS"] = "/home/ubuntu/HuffmanCoding/uploads"
@app.route("/compress", methods=["GET", "POST"])
def compress():
if request.method == "GET":
return render_template("compress.html", check=0)
else:
up_file = request.files["file"]
if len(up_file.filename) > 0:
global filename
global ftype
filename = up_file.filename
print(up_file.filename)
up_file.save(os.path.join(app.config["FILE_UPLOADS"], filename))
os.system('./c uploads/{}'.format(filename))
filename = filename[:filename.index(".",1)]
ftype = "-compressed.bin"
while True:
if 'uploads/{}-compressed.bin'.format(filename) in glob.glob('uploads/*-compressed.bin'):
os.system('mv uploads/{}-compressed.bin downloads/'.format(filename))
break
return render_template("compress.html", check=1)
else:
print("ERROR")
return render_template("compress.html", check=-1)
@app.route("/decompress", methods=["GET", "POST"])
def decompress():
if request.method == "GET":
return render_template("decompress.html", check=0)
else:
up_file = request.files["file"]
if len(up_file.filename) > 0:
global filename
global ftype
filename = up_file.filename
print(up_file.filename)
up_file.save(os.path.join(app.config["FILE_UPLOADS"], filename))
os.system('./d uploads/{}'.format(filename))
f = open('uploads/{}'.format(filename), 'rb')
ftype = "-decompressed." + (f.read(int(f.read(1)))).decode("utf-8")
filename = filename[:filename.index("-",1)]
while True:
if 'uploads/{}{}'.format(filename, ftype) in glob.glob('uploads/*-decompressed.*'):
os.system('mv uploads/{}{} downloads/'.format(filename, ftype))
break
return render_template("decompress.html", check=1)
else:
print("ERROR")
return render_template("decompress.html", check=-1)
@app.route("/download")
def download_file():
global filename
global ftype
path = "downloads/" + filename + ftype
return send_file(path, as_attachment=True)
# Restart application whenever changes are made
if __name__ == "__main__":
app.run(debug = True)