コード例 #1
0
ファイル: ViewFile.py プロジェクト: kriyazhao/Python-NFS
    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")
コード例 #2
0
ファイル: FileValidator.py プロジェクト: kriyazhao/Python-NFS
 def verifyfileContent(self, hashSHA1):
     if self.completePath != None:
         readfile = FileNotepad()
         readfile.setFilepath(self.completePath)
         encryptSHA1 = EncryptionPath()
         fileContent = readfile.readFile()
         # convert the hashed path and filename to hexadecimal numbers
         mySHA1 = encryptSHA1.creatSHA1(fileContent)
         if mySHA1 == hashSHA1:
             return True
         else:
             return False
コード例 #3
0
ファイル: CreateFile.py プロジェクト: kriyazhao/Python-NFS
    def post(self, request):

        readfile = FileNotepad()
        writenewfile = FileNotepad()
        encrypt = EncryptionPath()
        validator = FileValidator() # validate input

        if "username" in request.session:
            requestCon = json.loads(request.body)
            validator.setfileName(requestCon['fn'])
            if validator.verifyfileName() == True:
                with FileModel() as filemodel:
                    filemodel.setUsername(request.session["username"])
                    userID = filemodel.queryUserID()[0]
                hashMD5 = encrypt.creatMD5(requestCon['fn'], "", userID)
                hashSHA1 = encrypt.creatSHA1("")
                completePath = encrypt.writePath("get")
                validator.setfilePath(completePath)
                if validator.verifyfilePath() == False:
                    # write the file if not detected
                    logging.info("Adding a new fileContent at: {0}".format(completePath))
                    writenewfile.setFilepath(completePath)
                    writenewfile.writeNewFile("")
                    with FileModel() as filemodel:
                        filemodel.setUsername(request.session["username"])
                        filemodel.setFileName(requestCon['fn'])
                        filemodel.setUserID(userID)
                        filemodel.setHashCode(hashMD5, hashSHA1)
                        filemodel.insertFile()
                        newRow = filemodel.queryFile()
                        fileID = newRow["ID"]

                    # write log
                    with FileActivityLog() as activitylog:
                        activitylog.setUserID(newRow["userID"])
                        activitylog.setFileID(fileID)
                        activitylog.setCurName(requestCon['fn'])
                        activitylog.insertCreate()

                    return HttpResponse(json.dumps(newRow), content_type="application/json")
                else:
                    return HttpResponse(json.dumps('exist'), content_type="application/json")
            else:
                return HttpResponse(json.dumps('notvalid'), content_type="application/json")
        else:
            return HttpResponse(json.dumps('notlogin'), content_type="application/json")
コード例 #4
0
ファイル: UploadFile.py プロジェクト: kriyazhao/Python-NFS
    def post(self, request):
        readfile = FileNotepad()
        writenewfile = FileNotepad()
        filevalidator = FileValidator()
        encrypt = EncryptionPath()

        Filename = request.POST.get("Filename")
        rawData = request.FILES["Filedata"]
        if filevalidator.verifyfileSize(rawData) == False:
            return HttpResponse("exceed", content_type="text/plain")
        else:
            filevalidator.setfileName(Filename)
            if filevalidator.verifyfileName() == False:
                return HttpResponse("namenotvalid", content_type="text/plain")
            else:
                tempFile = self.saveFile(Filename, rawData)
                readfile.setFilepath(tempFile)
                Filedata = readfile.readRawFile()
                Filecontent = readfile.processRawFile(Filedata)
                if "username" in request.session:	
                    with FileModel() as filemodel:
                        filemodel.setUsername(request.session["username"])
                        results = filemodel.queryUserID()
                        userID = results[0]
                        filemodel.setFileName(Filename)
                        rows = filemodel.queryHashCode()
                    if rows != None:
                        os.remove(tempFile)
                        return HttpResponse("exist", content_type="text/plain")

                    hashMD5 = encrypt.creatMD5(Filename, Filecontent, userID)
                    hashSHA1 = encrypt.creatSHA1(Filecontent)
                    completePath = encrypt.writePath("get")
                    filevalidator.setfilePath(completePath)
                    if filevalidator.verifyfilePath() == False:
                        # write the file if not detected
                        logging.info("Adding a new fileContent at: {0}".format(completePath))
                        writenewfile.setFilepath(completePath)
                        writenewfile.writeNewFile(Filecontent)
                        with FileModel() as filemodel:
                            filemodel.setUserID(userID)
                            filemodel.setUsername(request.session["username"])
                            filemodel.setFileName(Filename)
                            filemodel.setHashCode(hashMD5, hashSHA1)
                            filemodel.insertFile()
                            fileID = filemodel.queryHashCode()[3]

                        # write log
                        with FileActivityLog() as activitylog:
                            activitylog.setUserID(userID)
                            activitylog.setFileID(fileID)
                            activitylog.setCurName(Filename)
                            activitylog.setOwnerID(userID)
                            activitylog.insertUpload()

                        # read the file again and verify it is hashed correctly
                        if filevalidator.verifyfileContent(hashSHA1) == True:
                            os.remove(tempFile)
                            return HttpResponse("success", content_type="text/plain")
                        else:
                            os.remove(tempFile)
                            logging.error("Something is weird about the provided: {0}".format(completePath))
                            return HttpResponse("internalerror", content_type="text/plain")
                    else:
                        os.remove(tempFile)
                        return HttpResponse("exist", content_type="text/plain")
                else:
                    os.remove(tempFile)
                    return HttpResponse("notlogin", content_type="text/plain")
コード例 #5
0
ファイル: EditFile.py プロジェクト: kriyazhao/Python-NFS
    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"] == "Yourself":
                ownerName = request.session["username"]
            else:
                ownerName = requestCon["owner"]

            with FileModel() as filemodel:
                filemodel.setUsername(ownerName)
                filemodel.setFileName(requestCon["filename"])
                rows = filemodel.queryHashCode()
                fileID  = rows[3]
                ownerID  = rows[0]
                filemodel.setUsername(request.session["username"])
                userID = filemodel.queryUserID()[0]
            if rows == None:
                return HttpResponse("notfound", content_type="text/plain")
            else:
                encrypt.setHashCode(rows[1], rows[2])
                completePath = encrypt.writePath("get")
                dirpath = encrypt.writePath("delete")
                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:
                    newMD5 = encrypt.creatMD5(requestCon["filename"], requestCon["content"], rows[0])
                    newSHA1 = encrypt.creatSHA1(requestCon["content"])
                    logging.info("newMD5: {0}".format(newMD5))
                    logging.info("newSHA1: {0}".format(newSHA1))
                    newPath = encrypt.writePath("get")
                    filevalidator.setfilePath(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(requestCon["content"])
                        with FileModel() as filemodel:
                            filemodel.setUsername(ownerName)
                            filemodel.setFileName(requestCon["filename"])
                            filemodel.setHashCode(newMD5, newSHA1)
                            filemodel.updateFile()

                        # 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["filename"])
                                activitylog.setOwnerID(ownerID)
                                activitylog.insertEdit()

                            return HttpResponse("success", content_type="text/plain")
                        else:
                            logging.error("Something is weird about the provided: {0}".format(newPath))
                            return HttpResponse("internalerror", content_type="text/plain")
                    else:
                        return HttpResponse("exist", content_type="text/plain")
        else:
            return HttpResponse("notlogin", content_type="text/plain")
コード例 #6
0
ファイル: RenameFile.py プロジェクト: kriyazhao/Python-NFS
    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")
コード例 #7
0
ファイル: DeleteFile.py プロジェクト: kriyazhao/Python-NFS
    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")
コード例 #8
0
ファイル: AppendFile.py プロジェクト: kriyazhao/Python-NFS
    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")