Beispiel #1
0
    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")
Beispiel #2
0
    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")