def post(self, request): if "username" in request.session: requestCon = json.loads(request.body) if requestCon["owner"] == "self": owner = request.session["username"] else: owner = requestCon["owner"] with FileModel() as filemodel: filemodel.setUsername(owner) filemodel.setFileName(requestCon["filename"]) rows = filemodel.queryHashCode() if rows == None: return HttpResponse(json.dumps('notfound'), content_type="application/json") else: with ShareFileModel() as sharefilemodel: sharefilemodel.setOwner(owner) sharefilemodel.setFileID(rows[3]) list = sharefilemodel.querySharedList_owner() logging.info("List:" + str(list)) dumpInfo = [{'md5':rows[1],'sha1':rows[2], 'owner':owner, 'privilege': rows[4]}] for item in list: dumpInfo.append(item) return HttpResponse(json.dumps(dumpInfo), content_type="application/json") else: return HttpResponse(json.dumps('notlogin'), content_type="application/json")
def get(self, request): if "username" in request.session: with FileModel() as filemodel: # query my list filemodel.setUsername(request.session["username"]) mylist = filemodel.queryList() logging.info(request.session["username"] + "'s My List: " + str(mylist)) with ShareFileModel() as sharefilemodel: # query shared list sharefilemodel.setSharedWith(request.session["username"]) sharedlist = sharefilemodel.querySharedList() logging.info(request.session["username"] + "'s Shared List: " + str(sharedlist)) if mylist != None or sharedlist != None: return HttpResponse(json.dumps([mylist,sharedlist]), content_type="application/json") else: return HttpResponse(json.dumps(['empty']), content_type="application/json") else: return HttpResponse(json.dumps(['empty']), content_type="application/json")
def post(self, request): readfile = FileNotepad() filevalidator = FileValidator() encrypt = EncryptionPath() if "username" in request.session: requestCon = json.loads(request.body) ownerName = requestCon["owner"] with ShareFileModel() as sharefilemodel: sharefilemodel.setFileAlias(requestCon["filename"]) sharefilemodel.setOwner(ownerName) sharefilemodel.setSharedWith(request.session["username"]) rows = sharefilemodel.queryShareFile_fileAlias() if rows != None: fileID = rows[1] if rows == None: return HttpResponse("notfound", content_type="text/plain") else: with FileModel() as filemodel: filemodel.setFileID(fileID) row = filemodel.queryFile_fileID() encrypt.setHashCode(row[1], row[2]) completePath = encrypt.writePath("get") filevalidator.setfilePath(completePath) if filevalidator.verifyfileContent(row[2]) == False: logging.error( "Something is weird about the provided: {0}".format( completePath)) return HttpResponse("internalerror", content_type="text/plain") else: readfile.setFilepath(completePath) fileContent = readfile.readFile() return HttpResponse(fileContent, content_type="text/plain") else: return HttpResponse("notlogin", content_type="text/plain")
def post(self, request): if "username" in request.session: requestCon = json.loads(request.body) md5validator = MD5Validator(requestCon["md5"]) if md5validator.MD5Validate() == True: with FileModel() as filemodel: filemodel.setHashCode(requestCon["md5"], requestCon["sha1"]) rows = filemodel.queryFile() fileID = rows["ID"] ownerID = rows["userID"] filemodel.setUserID(ownerID) ownerName = filemodel.queryUsername()[0] filemodel.setUsername(request.session["username"]) userID = filemodel.queryUserID()[0] if rows == None: return HttpResponse(json.dumps(['notfound']), content_type="application/json") else: with ShareFileModel() as sharefilemodel: sharefilemodel.setFileID(rows["ID"]) sharefilemodel.setOwner(ownerName) sharefilemodel.setSharedWith(request.session["username"]) logging.info(rows["ID"]) logging.info(ownerName) logging.info(request.session["username"]) sharefile = sharefilemodel.queryShareFile() sharefilemodel.setFileAlias(rows["filename"]) if sharefile == None: # not found in the sharefile table if ownerName == request.session["username"]: return HttpResponse(json.dumps(["owner"]), content_type="application/json") if rows["privilege"] == "11" or rows["privilege"] == "12": if rows["privilege"] == "11": sharefilemodel.setPrivilege(11) sharefilemodel.insertShareFile() elif rows["privilege"] == "12": sharefilemodel.setPrivilege(12) sharefilemodel.insertShareFile() shareDict={} shareDict["fileAlias"] = rows["filename"] shareDict["owner"] = ownerName shareDict["privilege"] = rows["privilege"] #write log with FileActivityLog() as activitylog: activitylog.setUserID(userID) activitylog.setFileID(fileID) activitylog.setCurName(rows["filename"]) activitylog.setOwnerID(ownerID) activitylog.insertExtract() return HttpResponse(json.dumps([shareDict]), content_type="application/json") elif rows["privilege"] == "2" or rows["privilege"] == "0": return HttpResponse(json.dumps(['restricted']), content_type="application/json") else: # found in the sharefile table if sharefile[6] == 1: sharefilemodel.setPrivilege(sharefile[5]) sharefilemodel.updateShareFile() shareDict={} shareDict["fileAlias"] = sharefile[2] shareDict["owner"] = ownerName shareDict["privilege"] = sharefile[5] #write log with FileActivityLog() as activitylog: activitylog.setUserID(userID) activitylog.setFileID(fileID) activitylog.setCurName(rows["filename"]) activitylog.insertExtract() return HttpResponse(json.dumps([shareDict]), content_type="application/json") else: return HttpResponse(json.dumps(['exist']), content_type="application/json") logging.info("1 file is found: {0}".format(sharefile)) else: return HttpResponse(json.dumps(['notvalid']), content_type="application/json") else: return HttpResponse(json.dumps(['notlogin']), content_type="application/json")
def post(self, request): if "username" in request.session: requestCon = json.loads(request.body) if "privilege" in requestCon: # anyone can share ownerName = requestCon["owner"] with FileModel() as filemodel: filemodel.setUsername(ownerName) filemodel.setFileName(requestCon["filename"]) rows = filemodel.queryHashCode() filemodel.setUsername(request.session["username"]) userID = filemodel.queryHashCode()[0] if rows != None: ownerID = rows[0] fileID = rows[3] if rows == None: return HttpResponse("notfound", content_type="text/plain") else: with FileModel() as filemodel: # change file privilege filemodel.setUsername(ownerName) filemodel.setFileName(requestCon["filename"]) if requestCon["privilege"] == 11: # anyone can view filemodel.updateShareOption(11) elif requestCon["privilege"] == 12: # anyone can edit filemodel.updateShareOption(12) # write log with FileActivityLog() as activitylog: activitylog.setUserID(userID) activitylog.setFileID(fileID) activitylog.setCurName(requestCon["filename"]) activitylog.setOwnerID(ownerID) activitylog.setSharedwithID(0) if requestCon["privilege"] == 11: # anyone can view activitylog.setSharedPrvlg(11) elif requestCon["privilege"] == 12: # anyone can edit activitylog.setSharedPrvlg(12) activitylog.insertShare() return HttpResponse("success", content_type="text/plain") else: # someone can share ownerName = requestCon[1]["un"] with FileModel() as filemodel: filemodel.setUsername(ownerName) filemodel.setFileName(requestCon[0]) rows = filemodel.queryHashCode() ownerID = rows[0] filemodel.setUsername(request.session["username"]) userID = filemodel.queryUserID()[0] if rows == None: return HttpResponse("notfound", content_type="text/plain") else: with FileModel() as filemodel: # change file privilege filemodel.setUsername(ownerName) filemodel.setFileName(requestCon[0]) fileID = filemodel.queryHashCode()[3] if len(requestCon) == 2: filemodel.updateShareOption(0) else: filemodel.updateShareOption(2) if len( requestCon ) > 2: # add/update share members, requestCon[1] is the owner sharelist = () for member in requestCon[2:]: sharelist = sharelist + (member["un"], ) with ShareFileModel() as sharefilemodel: sharefilemodel.setOwner(ownerName) sharefilemodel.setSharedWith(member["un"]) sharefilemodel.setFileID(fileID) sharefilemodel.setFileAlias(requestCon[0]) sharefilemodel.setPrivilege( member["privilege"]) shareRecord = sharefilemodel.queryShareFile() if shareRecord == None: sharefilemodel.insertShareFile() else: sharefilemodel.updateShareFile() with FileModel() as filemodel: filemodel.setUsername(member["un"]) sharedwithID = filemodel.queryUserID()[0] # write log with FileActivityLog() as activitylog: activitylog.setUserID(userID) activitylog.setFileID(fileID) activitylog.setCurName(requestCon[0]) activitylog.setOwnerID(ownerID) activitylog.setSharedwithID(sharedwithID) activitylog.setSharedPrvlg(member["privilege"]) activitylog.insertShare() with ShareFileModel( ) as sharefilemodel: # delete old share members sharefilemodel.setOwner(ownerName) sharefilemodel.setFileID(fileID) desharelist = sharefilemodel.deleteSharedWith( sharelist) for item in desharelist: # write log with FileActivityLog() as activitylog: activitylog.setUserID(userID) activitylog.setFileID(item["fileID"]) activitylog.setCurName(requestCon[0]) activitylog.setOwnerID(ownerID) activitylog.setSharedwithID( item["sharedwithID"]) activitylog.setSharedPrvlg(item["privilege"]) activitylog.insertDeletedShare() else: with ShareFileModel( ) as sharefilemodel: # delete old share members sharefilemodel.setOwner(ownerName) sharefilemodel.setFileID(fileID) desharelist = sharefilemodel.deleteSharedWith_owner( ) for item in desharelist: # write log with FileActivityLog() as activitylog: activitylog.setUserID(userID) activitylog.setFileID(item["fileID"]) activitylog.setCurName(requestCon[0]) activitylog.setOwnerID(ownerID) activitylog.setSharedwithID( item["sharedwithID"]) activitylog.setSharedPrvlg(item["privilege"]) activitylog.insertDeletedShare() return HttpResponse("success", content_type="text/plain") else: return HttpResponse("notlogin", content_type="text/plain")
def delete(self, request): readfile = FileNotepad() writenewfile = FileNotepad() filevalidator = FileValidator() encrypt = EncryptionPath() if "username" in request.session: requestCon = json.loads(request.body) if requestCon[ "owner"] == "self": # the owner asks to delete the file ownerName = request.session["username"] with FileModel() as filemodel: filemodel.setUsername(ownerName) filemodel.setFileName(requestCon["filename"]) rows = filemodel.queryHashCode() if rows != None: fileID = rows[3] userID = rows[0] if rows == None: return HttpResponse("notfound", content_type="text/plain") else: with ShareFileModel() as sharefilemodel: sharefilemodel.setFileID(fileID) sharefilemodel.setOwner(ownerName) desharelist = sharefilemodel.deleteSharedWith_owner() for item in desharelist: # write log with FileActivityLog() as activitylog: activitylog.setUserID(userID) activitylog.setFileID(item["fileID"]) activitylog.setCurName(requestCon["filename"]) activitylog.setOwnerID(userID) activitylog.setSharedwithID(item["sharedwithID"]) activitylog.setSharedPrvlg(item["privilege"]) activitylog.insertDeletedShare() encrypt.setHashCode(rows[1], rows[2]) completePath = encrypt.writePath("get") filevalidator.setfilePath(completePath) if filevalidator.verifyfileContent(rows[2]) == False: logging.error( "Something is weird about the provided: {0}". format(completePath)) return HttpResponse("internalerror", content_type="text/plain") else: with FileModel() as filemodel: filemodel.setUsername(ownerName) filemodel.setFileName(requestCon["filename"]) filemodel.setHashCode(rows[1], rows[2]) filemodel.deleteFile() dirpath = encrypt.writePath("delete") shutil.rmtree(dirpath) # write log with FileActivityLog() as activitylog: activitylog.setUserID(userID) activitylog.setFileID(fileID) activitylog.setCurName(requestCon["filename"]) activitylog.setOwnerID(userID) activitylog.insertDelete() return HttpResponse("success", content_type="text/plain") else: ownerName = requestCon["owner"] with ShareFileModel() as sharefilemodel: sharefilemodel.setFileAlias(requestCon["filename"]) sharefilemodel.setOwner(ownerName) sharefilemodel.setSharedWith(request.session["username"]) row = sharefilemodel.queryShareFile_fileAlias() if row != None: fileID = row[1] userID = row[4] ownerID = row[3] if row == None: return HttpResponse("notfound", content_type="text/plain") else: with FileModel() as filemodel: filemodel.setUsername(ownerName) filemodel.setFileName(requestCon["filename"]) rows = filemodel.queryHashCode() with ShareFileModel() as sharefilemodel: sharefilemodel.setFileID(fileID) sharefilemodel.setOwner(ownerName) sharefilemodel.setSharedWith( request.session["username"]) privilege = sharefilemodel.queryShareFile()[5] if privilege == 11 or privilege == 21: sharefilemodel.setFileAlias(requestCon["filename"]) desharelist = sharefilemodel.deleteSharedFile() elif privilege == 12 or privilege == 22: desharelist = sharefilemodel.deleteSharedWith_owner( ) for item in desharelist: # write log with FileActivityLog() as activitylog: activitylog.setUserID(userID) activitylog.setFileID(item["fileID"]) activitylog.setCurName( requestCon["filename"]) activitylog.setOwnerID(ownerID) activitylog.setSharedwithID( item["sharedwithID"]) activitylog.setSharedPrvlg( item["privilege"]) activitylog.insertDeletedShare() if privilege == 12 or privilege == 22: encrypt.setHashCode(rows[1], rows[2]) completePath = encrypt.writePath("get") filevalidator.setfilePath(completePath) if filevalidator.verifyfileContent(rows[2]) == False: logging.error( "Something is weird about the provided: {0}". format(completePath)) return HttpResponse("internalerror", content_type="text/plain") else: with FileModel() as filemodel: filemodel.setUsername(ownerName) filemodel.setFileName(requestCon["filename"]) filemodel.setHashCode(rows[1], rows[2]) filemodel.deleteFile() dirpath = encrypt.writePath("delete") shutil.rmtree(dirpath) # write log with FileActivityLog() as activitylog: activitylog.setUserID(userID) activitylog.setFileID(fileID) activitylog.setCurName(requestCon["filename"]) if privilege == 11 or privilege == 21: activitylog.setOwnerID(userID) if privilege == 12 or privilege == 22: activitylog.setOwnerID(ownerID) activitylog.insertDelete() return HttpResponse("success", content_type="text/plain") else: return HttpResponse("notlogin", content_type="text/plain")
def post(self, request): logging.info("Appending file...") readfile = FileNotepad() writenewfile = FileNotepad() filevalidator = FileValidator() encrypt = EncryptionPath() if "username" in request.session: requestCon = json.loads(request.body) if requestCon["owner"] == "self": ownerName = request.session["username"] with FileModel() as filemodel: filemodel.setUsername(ownerName) filemodel.setFileName(requestCon["filename"]) rows = filemodel.queryHashCode() userid = filemodel.queryUserID()[0] fileID = rows[3] else: ownerName = requestCon["owner"] with ShareFileModel() as sharefilemodel: sharefilemodel.setFileAlias(requestCon["filename"]) sharefilemodel.setOwner(ownerName) sharefilemodel.setSharedWith(request.session["username"]) fileID = sharefilemodel.queryShareFile_fileAlias()[1] with FileModel() as filemodel: filemodel.setFileID(fileID) rows = filemodel.queryFile_fileID() filemodel.setUsername(request.session["username"]) userid = filemodel.queryUserID()[0] encrypt.setHashCode(rows[1], rows[2]) completePath = encrypt.writePath("get") filevalidator.setfilePath(completePath) if filevalidator.verifyfileContent(rows[2]) == False: logging.error("Something is weird about the provided: {0}".format(completePath)) return HttpResponse(json.dumps(["internalerror"]), content_type="application/json") else: readfile.setFilepath(completePath) fileContent = readfile.readFile() count = 1 newName = requestCon["filename"] while rows != None: with FileModel() as filemodel: filemodel.setUsername(request.session["username"]) newName = requestCon["filename"][:-4] + " - Copy (" + str(count) + ").txt" filemodel.setFileName(newName) rows = filemodel.queryHashCode() count += 1 newMD5 = encrypt.creatMD5(newName, fileContent, userid) newSHA1 = encrypt.creatSHA1(fileContent) logging.info("newMD5: {0}".format(newMD5)) logging.info("newSHA1: {0}".format(newSHA1)) newPath = encrypt.writePath("get") filevalidator.setfilePath(newPath) logging.info("newfile: {0}".format(newPath)) if filevalidator.verifyfilePath() == False: # write the file if not detected logging.info("Adding a new fileContent at: {0}".format(newPath)) writenewfile.setFilepath(newPath) writenewfile.writeNewFile(fileContent) with FileModel() as filemodel: filemodel.setUsername(request.session["username"]) filemodel.setFileName(newName) filemodel.setUserID(userid) filemodel.setHashCode(newMD5, newSHA1) filemodel.insertFile() newfileID = filemodel.queryFile()["ID"] # write log with FileActivityLog() as activitylog: activitylog.setUserID(userid) activitylog.setFileID(newfileID) activitylog.setCurName(newName) activitylog.insertCreate() return HttpResponse(json.dumps(["success"]), content_type="application/json") else: return HttpResponse(json.dumps(["exist"]), content_type="application/json") else: return HttpResponse(json.dumps(["notlogin"]), content_type="application/json")
def post(self, request): readfile = FileNotepad() writenewfile = FileNotepad() filevalidator = FileValidator() encrypt = EncryptionPath() if "username" in request.session: requestCon = json.loads(request.body) if requestCon["owner"] == "self": ownerName = request.session["username"] with FileModel() as filemodel: filemodel.setUsername(ownerName) filemodel.setFileName(requestCon["oldname"]) rows = filemodel.queryHashCode() fileID = rows[3] userID = rows[0] ownerID = rows[0] hashMD5 = rows[1] hashSHA1 = rows[2] else: ownerName = requestCon["owner"] with ShareFileModel() as sharefilemodel: sharefilemodel.setFileAlias(requestCon["oldname"]) sharefilemodel.setOwner(ownerName) sharefilemodel.setSharedWith(request.session["username"]) rows = sharefilemodel.queryShareFile_fileAlias() fileID = rows[1] userID = rows[4] ownerID = rows[3] if rows == None: return HttpResponse(json.dumps(["notfound"]), content_type="application/json") else: privilege = "" if requestCon["owner"] != "self": with ShareFileModel() as sharefilemodel: sharefilemodel.setFileID(fileID) sharefilemodel.setOwner(ownerName) sharefilemodel.setSharedWith( request.session["username"]) privilege = sharefilemodel.queryShareFile()[5] with FileModel() as filemodel: filemodel.setFileID(fileID) result = filemodel.queryFile_fileID() hashMD5 = result[1] hashSHA1 = result[2] if privilege == 11 or privilege == 21 or privilege == 12 or privilege == 22: with ShareFileModel() as sharefilemodel: sharefilemodel.setFileID(fileID) sharefilemodel.setOwner(ownerName) sharefilemodel.setSharedWith( request.session["username"]) sharefilemodel.setFileAlias(requestCon["newname"]) sharefilemodel.setPrivilege(privilege) sharefilemodel.updateShareFile() if privilege == 11 or privilege == 21: # write log with FileActivityLog() as activitylog: activitylog.setUserID(userID) activitylog.setFileID(fileID) activitylog.setCurName(requestCon["newname"] + "/" + requestCon["oldname"]) activitylog.setOwnerID(ownerID) activitylog.insertRename() return HttpResponse(json.dumps(["success"]), content_type="application/json") if requestCon[ "owner"] == "self" or privilege == 12 or privilege == 22: encrypt.setHashCode(hashMD5, hashSHA1) completePath = encrypt.writePath("get") dirpath = encrypt.writePath("delete") filevalidator.setfilePath(completePath) if filevalidator.verifyfileContent(hashSHA1) == False: logging.error( "Something is weird about the provided: {0}". format(completePath)) return HttpResponse(json.dumps(["internalerror"]), content_type="application/json") else: readfile.setFilepath(completePath) fileContent = readfile.readFile() newMD5 = encrypt.creatMD5(requestCon["newname"], fileContent, ownerID) newSHA1 = encrypt.creatSHA1(fileContent) logging.info("newMD5: {0}".format(newMD5)) logging.info("newSHA1: {0}".format(newSHA1)) newPath = encrypt.writePath("get") filevalidator.setfilePath(newPath) logging.info("newfile: {0}".format(newPath)) if filevalidator.verifyfilePath() == False: # write the file if not detected logging.info( "Adding a new fileContent at: {0}".format( newPath)) writenewfile.setFilepath(newPath) writenewfile.writeNewFile(fileContent) with FileModel() as filemodel: filemodel.setUsername( request.session["username"]) filemodel.setFileName(requestCon["newname"]) filemodel.setHashCode(newMD5, newSHA1) filemodel.updateFile(requestCon["oldname"]) # read the file again and verify it is hashed correctly if filevalidator.verifyfileContent( newSHA1) == True: shutil.rmtree(dirpath) logging.info( "Deleting the old fileContent at: {0}". format(completePath)) # write log with FileActivityLog() as activitylog: activitylog.setUserID(userID) activitylog.setFileID(fileID) activitylog.setCurName( requestCon["newname"] + "/" + requestCon["oldname"]) activitylog.setOwnerID(ownerID) activitylog.insertRename() return HttpResponse( json.dumps(["success"]), content_type="application/json") else: logging.error( "Something is weird about the provided: {0}" .format(newPath)) return HttpResponse( json.dumps(["internalerror"]), content_type="application/json") else: return HttpResponse( json.dumps(["exist"]), content_type="application/json") else: return HttpResponse(json.dumps(["notlogin"]), content_type="application/json")