Skip to content

Commit

Permalink
fix download
Browse files Browse the repository at this point in the history
  • Loading branch information
ArceDanielShok committed Oct 9, 2024
1 parent cd67ced commit 4ae479d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 31 deletions.
10 changes: 8 additions & 2 deletions src/apps/main/device/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,14 @@ export async function downloadBackup(device: Device): Promise<void> {

const abortListener = (_: IpcMainEvent, abortDeviceUuid: string) => {
if (abortDeviceUuid === device.uuid) {
abortController.abort();
try {
Logger.info(`[BACKUPS] Aborting download for device ${device.name}`);
if (abortController && !abortController.signal.aborted) {
abortController.abort();
}
} catch (error) {
Logger.error(`[BACKUPS] Error while aborting download: ${error}`);
}
}
};

Expand All @@ -555,7 +562,6 @@ export async function downloadBackup(device: Device): Promise<void> {
await downloadDeviceBackupZip(device, zipFilePath, {
updateProgress: (progress: number) => {
if (abortController?.signal.aborted) return;
Logger.info(`[BACKUPS] Download progress: ${Math.round(progress)}`);
broadcastToWindows('backup-download-progress', {
id: device.uuid,
progress: Math.round(progress),
Expand Down
5 changes: 2 additions & 3 deletions src/apps/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,17 @@ app

eventBus.emit('APP_IS_READY');
const isLoggedIn = getIsLoggedIn();
setUpBackups();

if (!isLoggedIn) {
await createAuthWindow();
setTrayStatus('IDLE');
}

checkForUpdates();
})
.catch(Logger.error);

eventBus.on('WIDGET_IS_READY', () => {
setUpBackups();
});
eventBus.on('USER_LOGGED_IN', async () => {
try {
if (!AppDataSource.isInitialized) {
Expand Down
11 changes: 0 additions & 11 deletions src/apps/main/network/download.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Logger from 'electron-log';
import { FileVersionOneError } from '@internxt/sdk/dist/network/download';
import { FlatFolderZip } from './zip.service';
import { items } from '@internxt/lib';
Expand Down Expand Up @@ -47,8 +46,6 @@ export async function downloadFolderAsZip(
const writeStream = fs.createWriteStream(path + 'Backup_' + now + '.zip');
const destination = convertToWritableStream(writeStream);

Logger.info('Downloading folder as zip');

const { abortController, updateProgress } = opts;
const { bridgeUser, bridgePass, encryptionKey } = environment;
const { tree, folderDecryptedNames, fileDecryptedNames, size } =
Expand All @@ -59,22 +56,15 @@ export async function downloadFolderAsZip(
{ path: '', data: tree },
];

Logger.info('Creating zip file');

const zip = new FlatFolderZip(destination, {
abortController: opts.abortController,
progress: (loadedBytes) => {
Logger.info('Download progress', loadedBytes, size);
if (updateProgress) {
updateProgress((loadedBytes / size) * 100);
}
},
});

Logger.info('Adding files to zip');

Logger.info(!abortController?.signal.aborted);

while (pendingFolders.length > 0 && !abortController?.signal.aborted) {
const currentFolder = pendingFolders.shift() as {
path: string;
Expand Down Expand Up @@ -126,7 +116,6 @@ export async function downloadFolderAsZip(
throw new Error('Download cancelled');
}

Logger.info('Closing zip file');
return zip.close();
}

Expand Down
23 changes: 14 additions & 9 deletions src/apps/main/network/zip.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Logger from 'electron-log';
import { AsyncZipDeflate, Zip } from 'fflate';
import { ReadableStream, WritableStream } from 'node:stream/web';

Expand Down Expand Up @@ -26,18 +27,18 @@ export class FlatFolderZip {
private passThrough: ReadableStream<Uint8Array>;
private abortController?: AbortController;

constructor(destination: WritableStream<Uint8Array>, opts: FlatFolderZipOpts) {
constructor(
destination: WritableStream<Uint8Array>,
opts: FlatFolderZipOpts
) {
this.zip = createFolderWithFilesWritable(opts.progress);
this.abortController = opts.abortController;

this.passThrough = this.zip.stream;

this.finished = this.passThrough.pipeTo(
destination,
{
signal: opts.abortController?.signal,
}
);
this.finished = this.passThrough.pipeTo(destination, {
signal: opts.abortController?.signal,
});
}

addFile(name: string, source: ReadableStream<Uint8Array>): void {
Expand Down Expand Up @@ -78,15 +79,19 @@ export function createFolderWithFilesWritable(
},
cancel() {
if (passthroughController) {
passthroughController.close();
try {
passthroughController.close();
} catch (err) {
/* noop */
}
passthroughController = null;
}
},
});

zip.ondata = (err, data, final) => {
if (err) {
console.error('Error in ZIP data event:', err);
Logger.error('Error while zipping', err);
return;
}

Expand Down
6 changes: 2 additions & 4 deletions src/apps/renderer/hooks/backups/useBackupDownloadProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ export function useBackupDownloadProgress(): BackupDownloadContextProps {

useEffect(() => {
const removeListener = window.electron.onBackupDownloadProgress(
({ id, progress }: { id: string; progress: number }) => {
window.electron.logger.info(`Llego: ${progress}`);
({ id, progress }: { id: string; progress: number }) =>
setBackupDownloadProgress((prevState) => {
return { ...prevState, [id]: Math.round(progress) };
});
}
})
);
return removeListener;
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ export class ContentsDownloader {
});

downloader.on('progress', async () => {
Logger.debug('[Server] Download progress', filePath);

const stats = fs.statSync(filePath);
const fileSizeInBytes = stats.size;
const progress = fileSizeInBytes / file.size;
Expand Down

0 comments on commit 4ae479d

Please sign in to comment.