Instantly share code, notes, and snippets.

Embed
What would you like to do?
# ... Things I can't show ...
@app.route('/downloadSelected/<string:filenames>' , methods=['POST'])
def downloadSelected(filenames):
print("downloadSelected entered")
indexes = filenames[filenames.index('|indexes:') + len('|indexes:'): len(filenames)]
filenames = filenames[0:filenames.index('|indexes:')]
print(indexes + " :::: " + filenames)
session['downloaded'] = indexes
zip_folder(filenames, "collections/collections.zip")
data = ""
change_permissions_recursive("collections/collections")
with open('collections/collections' + '.zip' , 'rb') as fin:
data = io.BytesIO(fin.read())
data.seek(0)
"""mimetype='application/zip'"""
"""'text/html'"""
return send_file(
data,
mimetype= 'application/zip' ,
as_attachment=True,
attachment_filename='collections.zip'
)
@app.route('/deleteSelected/<string:filenames>' , methods=['GET', 'POST'])
def deleteSelected(filenames):
multipleSelected = True
filenames = filenames.split("%")
for filename in filenames:
print(filename)
shutil.rmtree('collections/' + (filename))
return redirect(url_for('listCollections'))
@app.route('/uploadSelected/<string:filenames>' , methods=['GET', 'POST'])
def uploadSelected(filenames):
res = 0
print("FILENAMES :::: " + filenames)
if(filenames.find('%') != -1):
filenames = filenames.split("%")
for filename in filenames:
if(uploadStatus('collections/' + filename) == "uploaded"):
filenames.remove(filename)
try:
uploadFiles(filenames, 1)
except Exception as ex:
print(ex)
pass
else:
if(uploadStatus('collections/' + filenames) != "uploaded"):
try:
uploadFiles([filenames], 0)
except Exception as ex:
print(ex)
pass
return redirect(url_for('listCollections'))
def uploadFiles(arrFilenames, arrCheck):
global client
client = MeteorClient('wss://www. ... .com')
matches = arrFilenames
total_size = 1
if(arrCheck == 0):
filename = matches[0]
if(uploadStatus('collections/' + filename) == "not_uploaded"):
total_size = getDirSize('collections/' + filename)
if total_size == 0 :
total_size = 1
if(uploadStatus('collections/' + filename) == "uploaded"):
matches.remove(filename)
return 0
else:
for filename in matches:
if(uploadStatus('collections/' + filename) == "not_uploaded"):
total_size = getDirSize('collections/' + filename)
if total_size == 0 :
total_size = 1
if(uploadStatus('collections/' + filename) == "uploaded"):
matches.remove(filename)
if(len(matches) == 0):
print("er102")
return 0
num_matches = len(matches)
matches.sort()
client.on('logged_in', logged_in)
client.on('subscribed', subscribed)
email = ' ... @ ...'
password = '...'
try:
client.connect()
client.login(email, password)
except Exception as e:
print(e)
pass
sleep_count = 0
while (not id or not company_id):
sleep_count += 1
time.sleep(1)
if(sleep_count > login_timeout):
if(not id):
print('failed to login')
elif(not company_id):
print('-1')
return
uploader = Uploader(client, 'Files', transport='http', verbose=False)
uploadedBytes = 0
realChunkCount = 0
os.system("echo 1 > upload.active.tmp; chmod 777 upload.active.tmp")
i = 0
for filename in matches:
problem_count = 0
previous_chunk = 0
collectionDir = "collections/" + filename
os.system('zip -r ' + filename + '.zip ' + collectionDir + '> /dev/null')
filepath = filename+'.zip'
fileID, chunkCount = uploader.upload(filepath, meta={"uploader_id":id, "company_id":company_id})
realChunkCount = realChunkCount + chunkCount
while not uploader.finished:
time.sleep(1)
problem_count += 1
if(problem_count > 30):
print('-4')
return
if(uploader.error):
print('-2')
return
if(uploader.currentChunk > previous_chunk):
problem_count = 0
uploadedBytes += (uploader.currentChunk - previous_chunk)*uploader.chunkSize
est = int(uploadedBytes / total_size * 100000) + 1000
if(est > 99999):
print('99999')
else:
print(est)
previous_chunk = uploader.currentChunk
os.system('rm ' + filename + '.zip ' + ' ; chown -R ...:... ' + collectionDir)
try:
tfile = open('upload.active.tmp', 'r')
active = tfile.readline()
tfile.close()
if(active[0] == '0'):
print('stopping upload...')
break
except IOError:
print('upload.active.tmp file corrupt')
i+=1
client.logout()
print('100')
return 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment