This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
app.py
332 lines (266 loc) · 10.4 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from flask import Flask, render_template, request, redirect, jsonify
import os
from werkzeug import secure_filename
import time
import pipes
import json
import requests
import ibm_boto3
from ibm_botocore.client import Config, ClientError
import math
app = Flask(__name__)
app.config["VIDEOS_UPLOAD"] = "static/raw/"
app.config["AUDIO_FILES"] = "static/audios/"
app.config["COS_VIDEOS"] = "videos/"
app.config["COS_AUDIOS"] = "audios/"
# Constants for IBM COS values
COS_ENDPOINT = ""
COS_API_KEY_ID = ""
COS_AUTH_ENDPOINT = ""
COS_RESOURCE_CRN = ""
COS_BUCKET_LOCATION = "us-standard"
bucket_name = ""
'''Cloud Object Storage Methods'''
with open('credentials.json', 'r') as credentialsFile:
credentials = json.loads(credentialsFile.read())
# connect to IBM cloud object storage
endpoints = requests.get(credentials.get('endpoints')).json()
iam_host = (endpoints['identity-endpoints']['iam-token'])
cos_host = (endpoints['service-endpoints']
['cross-region']['us']['public']['us-geo'])
# Assign Bucket Name
try:
bucket_name = credentials.get('bucket_name')
except Exception as e:
bucket_name = "notassigned"
# Constrict auth and cos endpoint
auth_endpoint = "https://" + iam_host + "/oidc/token"
service_endpoint = "https://" + cos_host
# Constants for IBM COS values
COS_ENDPOINT = service_endpoint
COS_API_KEY_ID = credentials.get('apikey')
COS_AUTH_ENDPOINT = auth_endpoint
# eg "crn:v1:bluemix:public:cloud-object-storage:global:a/3bf0d9003abfb5d29761c3e97696b71c:d6f04d83-6c4f-4a62-a165-696756d63903::"
COS_RESOURCE_CRN = credentials.get('resource_instance_id')
# Create client
cos = ibm_boto3.resource("s3",
ibm_api_key_id=COS_API_KEY_ID,
ibm_service_instance_id=COS_RESOURCE_CRN,
ibm_auth_endpoint=COS_AUTH_ENDPOINT,
config=Config(signature_version="oauth"),
endpoint_url=COS_ENDPOINT
)
@app.route('/COSBucket', methods=['GET', 'POST'])
def setupCOSBucket():
if request.method == 'POST':
temp = request.form
bkt = json.loads(temp['bkt'])
with open('credentials.json', 'r') as credentialsFile:
cred = json.loads(credentialsFile.read())
cred.update(bkt)
print(json.dumps(cred, indent=2))
with open('credentials.json', 'w') as fp:
json.dump(cred, fp, indent=2)
return jsonify({'flag': 0})
@app.route('/initCOS')
def initializeCOS():
try:
global bucket_name
flag = False
buckets = cos.buckets.all()
with open('credentials.json', 'r') as credentialsFile:
cred = json.loads(credentialsFile.read())
for bucket in buckets:
if cred['bucket_name'] == bucket.name:
flag = True
bucket_name = cred['bucket_name']
break
if not flag:
bucket_name = cred['bucket_name']
respo = create_bucket(bucket_name)
else:
respo = {"message": "Bucket \"" + bucket_name + "\" found!"}
except ClientError as be:
respo = {"message": "CLIENT ERROR: {0}\n".format(be)}
except Exception as e:
respo = {"message": " {0}".format(e)}
return json.dumps(respo, indent=2)
def create_bucket(bucket_name):
print("Creating new bucket: {0}".format(bucket_name))
try:
cos.Bucket(bucket_name).create(
CreateBucketConfiguration={
"LocationConstraint": COS_BUCKET_LOCATION
}
)
respo = {"message": "Bucket: {0} created!".format(bucket_name)}
return respo
except ClientError as be:
print("CLIENT ERROR: {0}\n".format(be))
respo = {"message": "CLIENT ERROR: {0}\n".format(be)}
return respo
except Exception as e:
print("Unable to create bucket: {0}".format(e))
respo = {"message": "Unable to create bucket: {0}".format(e)}
return respo
def get_bucket_contents(bucket_name):
myList = []
print("Retrieving bucket contents from: {0}".format(bucket_name))
try:
files = cos.Bucket(bucket_name).objects.all()
for file in files:
myList.append([file.key, file.size])
print("Item: {0} ({1} bytes).".format(file.key, file.size))
return myList
except ClientError as be:
print("CLIENT ERROR: {0}\n".format(be))
except Exception as e:
print("Unable to retrieve bucket contents: {0}".format(e))
def get_item(bucket_name, item_name):
print("Retrieving item from bucket: {0}, key: {1}".format(
bucket_name, item_name))
try:
file = cos.Object(bucket_name, item_name).get()
return file["Body"].read()
except ClientError as be:
print("CLIENT ERROR: {0}\n".format(be))
except Exception as e:
print("Unable to retrieve file contents: {0}".format(e))
def delete_item(bucket_name, item_name):
print("Deleting item: {0}".format(item_name))
try:
cos.Object(bucket_name, item_name).delete()
print("Item: {0} deleted!".format(item_name))
except ClientError as be:
print("CLIENT ERROR: {0}\n".format(be))
except Exception as e:
print("Unable to delete item: {0}".format(e))
def multi_part_upload(bucket_name, item_name, file_path):
try:
print("Starting file transfer for {0} to bucket: {1}\n".format(
item_name, bucket_name))
# set 5 MB chunks
part_size = 1024 * 1024 * 5
# set threadhold to 15 MB
file_threshold = 1024 * 1024 * 15
# set the transfer threshold and chunk size
transfer_config = ibm_boto3.s3.transfer.TransferConfig(
multipart_threshold=file_threshold,
multipart_chunksize=part_size
)
# the upload_fileobj method will automatically execute a multi-part upload
# in 5 MB chunks for all files over 15 MB
with open(file_path, "rb") as file_data:
cos.Object(bucket_name, item_name).upload_fileobj(
Fileobj=file_data,
Config=transfer_config
)
print("Transfer for {0} Complete!\n".format(item_name))
except ClientError as be:
print("CLIENT ERROR: {0}\n".format(be))
except Exception as e:
print("Unable to complete multi-part upload: {0}".format(e))
'''Flask Helper Methods'''
def scanAvailableVideoFiles():
availableFiles = os.listdir(app.config["VIDEOS_UPLOAD"])
return availableFiles
def scanAvailableAudioFiles():
availableFiles = os.listdir(app.config["AUDIO_FILES"])
return availableFiles
def videoToAudio():
for file in scanAvailableVideoFiles():
print(file)
if file.split('.')[0] in [x.split('.')[0] for x in scanAvailableAudioFiles()]:
print("file already converted! Skipping...")
myFlag = {"flag": 0}
continue
else:
fileName = app.config["VIDEOS_UPLOAD"] + file
try:
file, file_extension = os.path.splitext(fileName)
file = pipes.quote(file)
filename = file.split('/')
video_to_wav = 'ffmpeg -i ' + file + file_extension + \
' -vn -f flac -ab 192000 -vn ' + \
app.config["AUDIO_FILES"] + filename[2] + '.flac'
print(video_to_wav)
os.system(video_to_wav)
item_name = app.config["COS_AUDIOS"] + filename[2] + '.flac'
file_path = app.config["AUDIO_FILES"] + filename[2] + '.flac'
multi_part_upload(bucket_name, item_name, file_path)
myFlag = {"flag": 0}
except OSError as err:
myFlag = {"flag": 1}
return json.dumps(myFlag, indent=2)
def deleteFiles(fileName, fileType):
try:
fileNameLocal = fileName.split('/')[1]
if fileType == 'video':
fileToDelete = 'rm static/raw/' + fileNameLocal
elif fileType == 'audio':
fileToDelete = 'rm static/audios/' + fileNameLocal
os.system(fileToDelete)
item_name = fileName
delete_item(bucket_name, item_name)
myFlag = {"flag": 0}
except OSError as err:
myFlag = {"flag": 1}
return json.dumps(myFlag, indent=2)
@app.route('/uploader', methods=['GET', 'POST'])
def uploader():
try:
if request.method == 'POST':
if request.files:
videos = request.files.getlist("video")
for f in videos:
filename_converted = f.filename.replace(
" ", "-").replace("'", "").lower()
f.save(os.path.join(
app.config["VIDEOS_UPLOAD"], secure_filename(filename_converted)))
item_name = app.config["COS_VIDEOS"]+filename_converted
file_path = app.config["VIDEOS_UPLOAD"]+filename_converted
multi_part_upload(bucket_name, item_name, file_path)
myResponse = {"message": 1}
except Exception as e:
print("Unable {0}".format(e))
myResponse = {"message": str(e)}
return json.dumps(myResponse, indent=2)
@app.route('/getAudioFiles')
def getAudioFiles():
jsonList = []
for file in get_bucket_contents(bucket_name):
if file[0][0] == 'a':
myDict = {'audioFile': file[0], 'fileSize': convert_size(file[1])}
jsonList.append(myDict)
return json.dumps(jsonList, indent=2)
@app.route('/getVideoFiles')
def getVideoFiles():
jsonList = []
for file in get_bucket_contents(bucket_name):
if file[0][0] == 'v':
myDict = {'videoFile': file[0], 'fileSize': convert_size(file[1])}
jsonList.append(myDict)
return json.dumps(jsonList, indent=2)
@app.route('/convert')
def convert():
return videoToAudio()
@app.route('/deleteUploadedFile')
def deleteUploadedFile():
fileName = request.args['fileName']
fileType = request.args['fileType']
return deleteFiles(fileName, fileType)
@app.route('/')
def index():
return render_template('index.html')
def convert_size(size_bytes):
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return "%s %s" % (s, size_name[i])
port = os.getenv('VCAP_APP_PORT', '8080')
if __name__ == "__main__":
app.secret_key = os.urandom(12)
app.run(debug=True, host='0.0.0.0', port=port)