def handle(self): toDeletePath = self._task.data["fullPath"] dbAccounts = {acc.id: acc for acc in self._databaseAccess.getAllAccounts()} cachedFileInfo = self._filesCache.getFile(toDeletePath) if cachedFileInfo: for partName, part in cachedFileInfo.parts.items(): self._logger.debug(f"Removing part: {partName} from accountID: {part.storingAccountID}") cloudAccount = CloudAPIFactory.fromAccountData(dbAccounts[part.storingAccountID]) cloudAccount.deleteFile(part) self._filesCache.removeFile(toDeletePath)
def __uploadToFirstAccountOnly(self, account, fileHandle, cachedFile): if not self._task.stale: if cachedFile: self.__cleanFromRemote(cachedFile) self._filesCache.removeFile(cachedFile.data.fullPath) cloudAccount = CloudAPIFactory.fromAccountData(account) cloudFileName = f"{self._task.data['filename']}__1__1.enc" result = cloudAccount.upload(fileHandle, self._task.data['size'], cloudFileName, self._task) if result: self.__updateFilesCache(result)
def __moveFile(self, cachedSourceFile, movedFileData): storedParts = {partInfo.storingAccountID: partInfo for partName, partInfo in cachedSourceFile.parts.items()} cloudAccounts = [CloudAPIFactory.fromAccountData(account) for account in self._databaseAccess.getAllAccounts() if account.id in storedParts] for account in cloudAccounts: part = storedParts[account.accountData.id] # alma__1__2.enc newPartName = self.__getNewPartName(part.filename, movedFileData["filename"]) newPartFullPath = f"{movedFileData['path']}/{newPartName}" if len(movedFileData['path']) > 0 else newPartName account.moveFile(part, newPartFullPath) part.path = movedFileData["path"] part.fullPath = newPartFullPath cachedSourceFile.data.path = movedFileData["path"] cachedSourceFile.data.fullPath = movedFileData["fullPath"] self._filesCache.moveFile(self._task.data["source"], movedFileData["fullPath"])
def __uploadToAllAccounts(self, accounts, perAccountSize, fileHandle, cachedFile): if not self._task.stale: if cachedFile: self.__cleanFromRemote(cachedFile) self._filesCache.removeFile(cachedFile.data.fullPath) totalCount = len(accounts) for account, partIndex, toUploadChunkInfo in zip(accounts, range(totalCount), chunkSizeGenerator(self._task.data["size"], perAccountSize)): if not self._task.stale: cloudAccount = CloudAPIFactory.fromAccountData(account) cloudFileName = f"{self._task.data['filename']}__{partIndex + 1}__{totalCount}.enc" result = cloudAccount.upload(fileHandle, toUploadChunkInfo[0], cloudFileName, self._task) if result: self._logger.debug(f"Updating cache with result: {result}") self.__updateFilesCache(result)
def handle(self): localFilePath = f"{control.cli.CONSOLE_ARGUMENTS.workspace}/server/{self._task.uuid}" targetFilePath = f"{control.cli.CONSOLE_ARGUMENTS.workspace}/client/{self._task.uuid}" cachedFileInfo = self._filesCache.getFile(self._task.data["fullPath"]) parts = [part for key, part in cachedFileInfo.parts.items()] parts.sort(key=lambda part: part.filename) storingAccounts = {account.id: account for account in self._databaseAccess.getAllAccounts() if account.id in [part.storingAccountID for part in parts]} self._logger.debug(f"Downloading file '{cachedFileInfo.data.fullPath}' from accounts: {[acc.identifier for key, acc in storingAccounts.items()]}") self._logger.debug(f"Sorted parts: {parts}") with open(localFilePath, "wb") as outputFileHandle: for part in parts: cloudAccount = CloudAPIFactory.fromAccountData(storingAccounts[part.storingAccountID]) self._logger.debug(f"Downloading part {part.filename} from {cloudAccount.accountData.identifier}") cloudAccount.download(outputFileHandle, part, self._task) self._logger.debug("Download finished, moving file to client workspace...") self.__finalizeDownload(localFilePath, targetFilePath) self._task = None
def __cleanFromRemote(self, cachedFile): storedParts = {partInfo.storingAccountID: partInfo for partName, partInfo in cachedFile.parts.items()} cloudAccounts = [CloudAPIFactory.fromAccountData(account) for account in self._databaseAccess.getAllAccounts() if account.id in storedParts] for account in cloudAccounts: account.deleteFile(storedParts[account.accountData.id])
def __init__(self, *args): super().__init__(*args) self.__cloudAccounts = [CloudAPIFactory.fromAccountData(accData) for accData in self._databaseAccess.getAllAccounts()] self.__totalCountPattern = "__[0-9]+" self._filesCache = CloudFilesCache()