-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
141 lines (110 loc) · 3.7 KB
/
Copy pathapp.py
File metadata and controls
141 lines (110 loc) · 3.7 KB
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
from flask_cors import CORS
from flask import Flask, request, jsonify, send_file, request
from werkzeug.utils import secure_filename
import os
import classBased as cb
import huffmanFiles as hfile
import json
import uuid
hf = cb.Huffboth()
decode_dir = 'decoded'
encodedCompressed_dir = 'encodedCompressed'
encodedUncompressed_dir = 'encodedUncompressed'
app = Flask(__name__)
CORS(app)
###############################################################
# Test Routes
@app.route('/')
def index():
return "<h1>Welcome to our server !!</h1>"
@app.route('/home')
def index2():
return "<h1>Welcome to Home server !!</h1>"
@app.route('/api/home')
def index3():
return jsonify({
"home": "This is home sweet home",
})
###############################################################
# Text Compression Decompression Routes
@app.route("/api/huffman-text-encode", methods=["POST"])
def huffmanTextEncode():
# print(request.get_json())
obj = request.get_json()
input = obj["payload"]
# print(input)
inputSize = len(input) * 8
encoded = hf.Encode(input)
outputSize = len(encoded)
tree = hf.getTree()
compressionRatio = inputSize/outputSize
return jsonify({
"output": encoded,
'inputSize': inputSize,
"outputSize": outputSize,
"tree": tree,
"compressionRatio": round(compressionRatio, 2)
})
@app.route("/api/huffman-text-decode", methods=["POST"])
def huffmanTextDecode():
# print(request.get_json())
obj = request.get_json()
input = obj["payload"]
inputSize = len(input)
# print(input)
decoded = hf.Decode(input)
tree = hf.getTree()
outputSize = len(decoded) * 8
compressionRatio = inputSize/outputSize
return jsonify({
"output": decoded,
'inputSize': inputSize,
"outputSize": outputSize,
"compressionRatio": round(compressionRatio, 2),
"tree": tree,
})
@app.route("/api/get-tree", methods=["GET"])
def sendHuffmanTree():
tree = hf.getTree()
return jsonify({
"tree": tree,
})
###############################################################
# File Upload routes for normal and binary files
@app.route('/api/upload-normal-file', methods=["POST"])
def uploadNormalFile():
if request.method == 'POST':
fileID = uuid.uuid4()
f = request.files['file']
f.save(os.path.join(decode_dir, f"{fileID}.txt"))
hfile.encodeHuffFile(fileID)
return 'normal file uploaded successfully'
return "Normal File Upload Route"
@app.route('/api/upload-encoded-compressed-file', methods=["POST"])
def uploadEncodedCompressedFile():
if request.method == 'POST':
fileID = uuid.uuid4()
f = request.files['file']
f.save(os.path.join(encodedCompressed_dir, f"{fileID}.bin"))
hfile.decodeHuffFile(fileID)
return 'binary file uploaded successfully'
return "Binary File Upload Route"
###############################################################
# Get the files by ID
@app.route('/decoded/<ID>')
def getDecodedFile(ID):
filename = f'{decode_dir}/image-{ID}.png'
return send_file(filename, mimetype='image/png')
@app.route('/encoded-compressed/<ID>')
def getEncodedCompressedFile(ID):
filename = f'{encodedCompressed_dir}/image-{ID}.png'
return send_file(filename, mimetype='image/png')
@app.route('/encoded-uncompressed/<ID>')
def getEncodedUnompressedFile(ID):
filename = f'{encodedUncompressed_dir}/image-{ID}.png'
return send_file(filename, mimetype='image/png')
#################################################################
if __name__ == '__main__':
app.config['ENV'] = 'development'
app.config['DEBUG'] = True
app.run(threaded=True, port=5000, debug=True)