예제 #1
0
def deleteFile(fileName):

    if os.path.isfile("metadata.json"):
        f = open("metadata.json", "r")
        readJSON = json.load(f)
        f.close()

        print ("[SYSTEM] Read metadata, Start remove chunk file")
        idx = 0
        item = 0

        while 1:
            if readJSON[item]['fileName'] == fileName:
                if readJSON[item]['indexOfChunk'] == idx:
                    lastIdx = readJSON[item]['numberOfChunks']
                    indexOfDot = fileName.index('.')
                    name = fileName[:indexOfDot]

                    for key_account in readJSON[item]['origin']:
                        account = key_account
                        service = credentials.get_service(account)
                        googleDrive.delete_file(service, readJSON[item]['origin'][account])

                        log = "delete_" + name + '_' + str(idx) + '_' + account + '_origin'
                        googleDrive.write_log(log)

                    for key_account in readJSON[item]['replication']:
                        account = key_account
                        service = credentials.get_service(account)
                        googleDrive.delete_file(service, readJSON[item]['replication'][account])

                        log = "delete_" + name + '_' + str(idx) + '_' + account + '_replication'
                        googleDrive.write_log(log)

                    print("[DELETE] Removed chunk({0}) file - {1}\n".format(idx+1, readJSON[item]['chunkName']))
                    del readJSON[item]
                    idx += 1
                    item = 0
                    if idx == lastIdx:
                        print ("[DELETE] Deleted file on google drive - '%s'" % fileName)
                        break
                else:
                    item += 1
                    if item > len(readJSON)-1:
                            print ("[ERROR ] This metadata is wrong! Please check your metadata")
                            print ("         Doesn't exist metadata of {0} 's chunk({1}) file".format(fileName, idx))
                            sys.exit(0)
            else:
                item += 1
                if item > len(readJSON)-1:
                            print ("[ERROR ] This metadata is wrong! Please check your metadata")
                            print ("         Doesn't exist metadata of {0} 's chunk({1}) file".format(fileName, idx))
                            sys.exit(0)

        if len(readJSON) == 0:
            os.remove("metadata.json")
            print ("[DELETE] Delete metadata.json")
        else:
            # Create metadata.json
            f = open('metadata.json', 'w')
            json.dump(readJSON, f, indent=4)
            f.close()
            print ("[SYSTEM] Remove metadata of '%s'" % fileName)

    else:
        print("[ERROR ] Doesn't exist metadata.json")
예제 #2
0
def uploadGoogledrive(accountSortList, account, flag):

    uploadfilePath = os.getcwd() + "/cache/"
    service = credentials.get_service(account)
    folderID = googleDrive.get_shared_folder_id(service, account)
    two_time = 0

    dt_obj = datetime.now()
    dt_obj = dt_obj.replace(day=dt_obj.day + 7)
    nowDate = datetime.strftime(dt_obj, '%Y_%m_%d')

    daily_folder_id = googleDrive.check_daily_folder_and_get_id(service, account, nowDate, folderID)

    for i in range(0, len(accountSortList)):
        fn1 = accountSortList[i]['chunkName']
        fileName = nowDate + '_' + fn1

        garbage_log = account + ' ' + fileName
        googleDrive.write_garbage_log(garbage_log)

        if os.path.isfile(uploadfilePath+fn1):
            # upload_file(업로드된후 파일명, 파일설명, 파일타입, 실제 올릴 파일경로)
            uploadFile = googleDrive.upload_file(service, daily_folder_id, "%s" % fileName, '', '', uploadfilePath + fn1)

            if type(uploadFile) == 'NoneType':
                 print ("[ERROR ] Failed upload Chunk({0})- '{1}'".format(accountSortList[i]['indexOfChunk']+1, fn1))

            else:
                if flag == "origin":
                    global infoList

                    log = "upload_" + fn1 + '_' + account + "_origin"
                    googleDrive.write_log(log)

                    if accountSortList[i]['origin'][account] == "null":
                        accountSortList[i]['origin'][account] = uploadFile['id']
                        accountSortList[i]['uploadTime'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                        infoList.append(accountSortList[i])

                        print ("[SYSTEM] Stored metadata of original Chunk({0})- '{1}'".format(accountSortList[i]['indexOfChunk']+1, fn1))

                elif flag == "replication":
                    global finished_infoList

                    log = "upload_" + fn1 + '_' + account + "_replication"
                    googleDrive.write_log(log)

                    if accountSortList[i]['replication'][account] == "null":
                        accountSortList[i]['replication'][account] = uploadFile['id']

                        # Delete already uploaded chunk file
                        for key in accountSortList[i]['replication']:
                            if key != account:
                                if accountSortList[i]['replication'][key] != "null":
                                    if fileName == uploadFile['title']:
                                        two_time += 1
                        if two_time == 1:
                            two_time = 0
                            finished_infoList.append(accountSortList[i])
                            print ("[SYSTEM] Stored metadata of replication of Chunk({0})- '{1}'".format(accountSortList[i]['indexOfChunk']+1, fn1))
                            os.remove(uploadfilePath+fn1)
                            print ("[DELETE] Remove already uploaded chunk file - '{0}'".format(fn1))
        else:
            print ("[ERROR ] Failed upload Chunk({0})- '{1}'".format(accountSortList[i]['indexOfChunk']+1, fn1))