# Python imports import os, hashlib, json, base64 # Flask imports from flask import Flask, request, render_template, session, send_from_directory, url_for, redirect from flask_uploads import UploadSet, configure_uploads, ALL from werkzeug.utils import secure_filename # Application Imports from . import app from .MessageHandler import MessageHandler msgHandler = MessageHandler() SCRIPT_PTH = os.path.dirname(os.path.realpath(__file__)) + "/" HOME_PTH = os.path.expanduser("~") NOTES_PTH = SCRIPT_PTH + 'static/' + "NOTES.txt" UPLOADS_PTH = HOME_PTH + '/Downloads/' files = UploadSet('files', ALL, default_dest=lambda x: UPLOADS_PTH) list = [] # stores file name and hash configure_uploads(app, files) # Load notes... notesListEncoded = [] notesListDecoded = [] with open(NOTES_PTH) as infile: try: notesJson = json.load(infile) for entry in notesJson: notesListEncoded.append(entry) entryDecoded = str(base64.urlsafe_b64decode( entry.encode("utf-8") ), "utf-8") notesListDecoded.append(entryDecoded) except Exception as e: print(e) # Empty route used for url_for route @app.route('/uploads', methods=['GET', 'POST']) def uploads(): return "" @app.route('/', methods=['GET', 'POST']) def root(): # Get file list del list[:] files = os.listdir(UPLOADS_PTH) for file in files: fpth = UPLOADS_PTH + file if not os.path.isdir(fpth): # ignore directories hash = hash_file(fpth) list.append([file, hash]) return render_template('index.html', title="Dropper", files=list, notes=notesListDecoded) @app.route('/upload', methods=['GET', 'POST']) def upload(): if request.method == 'POST' and len(request.files) > 0: for rFile in request.files: try: files.save(request.files[rFile]) except Exception as e: msg = "[Error] Failed to Uploaded file(s)..." return msgHandler.createMessageJSON("danger", msg) msg = "[Success] Uploaded file(s)..." return msgHandler.createMessageJSON("success", msg) else: msg = "Use POST request and send data...." return msgHandler.createMessageJSON("danger", msg) @app.route('/edit-file', methods=['GET', 'POST']) def editFile(): hash = request.values['hash'].strip() newName = request.values['newName'].strip() if request.method == 'POST' and hash != '' and newName != '': editedFile = False fname = None errorMsg = None for file in list: if file[1] == hash: try: oldPath = UPLOADS_PTH + file[0] newPath = UPLOADS_PTH + newName os.rename(oldPath, newPath) editedFile = True fname = file[0] file[0] = newName except Exception as e: errorMsg = str(e) print("Exception for editing action:") print(e) if editedFile: msg = "[Success] Renamed File!" return msgHandler.createMessageJSON("success", msg) else: msg = "[Error] Failed to edit file name!" return msgHandler.createMessageJSON("danger", msg) else: msg = "Use POST request and send data...." return msgHandler.createMessageJSON("danger", msg) @app.route('/delete-file', methods=['GET', 'POST']) def deleteFile(): hash = request.values['hash'].strip() if request.method == 'POST' and hash != '': deletedFile = False fname = None errorMsg = None for file in list: if file[1] == hash: try: path = UPLOADS_PTH + file[0] os.remove(path) deletedFile = True fname = file[0] except Exception as e: print("Exception for deletiong action:") print(e) errorMsg = str(e) if deletedFile: msg = "[Success] Deleted file..." return msgHandler.createMessageJSON("success", msg) else: msg = "[Error] Failed to delete file..." return msgHandler.createMessageJSON("danger", msg) else: msg = "Use POST request and send data...." return msgHandler.createMessageJSON("danger", msg) @app.route('/delete-text', methods=['GET', 'POST']) def deleteText(): if request.method == 'POST': try: encodedNote = request.values['encodedstr'].strip() decodedNote = str(base64.urlsafe_b64decode( encodedNote.encode("utf-8") ), "utf-8") notesListEncoded.remove(encodedNote) notesListDecoded.remove(decodedNote) updateNotesFile() msg = "[Success] Deleted entry..." return msgHandler.createMessageJSON("success", msg) except Exception as e: print(e) msg = "[Error] Failed to delete entry!" return msgHandler.createMessageJSON("danger", msg) @app.route('/delete-all-text', methods=['GET', 'POST']) def deleteAllText(): if request.method == 'POST': try: open(NOTES_PTH, 'w').close() msg = "[Success] Deleted all enteries..." return msgHandler.createMessageJSON("success", msg) except Exception as e: print(e) msg = "[Error] Failed to delete all enteries!" return msgHandler.createMessageJSON("danger", msg) @app.route('/add-note', methods=['GET', 'POST']) def addNote(): text = request.values['entryText'].strip() if request.method == 'POST' and text != '': try: encodedBytes = base64.urlsafe_b64encode(text.encode("utf-8")) encodedStr = str(encodedBytes, "utf-8") notesListDecoded.append(text) notesListEncoded.append(encodedStr) updateNotesFile() msg = "[Success] Added text entry!" return msgHandler.createMessageJSON("success", msg) except Exception as e: print("Exception for file write action:") print(e) msg = "[Error] A text entry write error occured!" return msgHandler.createMessageJSON("danger", msg) else: msg = "Use POST request and send data...." return msgHandler.createMessageJSON("danger", msg) @app.route('/uploads/') def returnFile(id): return send_from_directory(UPLOADS_PTH, id) def updateNotesFile(): with open(NOTES_PTH, 'w') as file: file.write( json.dumps(notesListEncoded, indent=4) ) file.close() """"This function returns the SHA-1 hash of the file passed into it""" def hash_file(filename): # Make a hash object h = hashlib.sha1() # Open file for reading in binary mode with open(filename,'rb') as file: # Loop till the end of the file chunk = 0 while chunk != b'': # Read only 1024 bytes at a time chunk = file.read(1024) h.update(chunk) # Return the hex representation of digest return h.hexdigest()