Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows zip files to be selected for archive imprt #345

Merged
merged 6 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/account_x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ export class XAccountController {
) as Sqlite3Count;
const totalUnknownIndexed: Sqlite3Count = exec(
this.db,
`SELECT COUNT(*) AS count FROM tweet
`SELECT COUNT(*) AS count FROM tweet
WHERE id NOT IN (
SELECT id FROM tweet WHERE username = ? AND text NOT LIKE ? AND isLiked = ?
UNION
Expand Down Expand Up @@ -1750,6 +1750,28 @@ export class XAccountController {
return null;
}


// Unzip twitter archive to the account data folder using unzipper
// Return unzipped path if success, else null.
async unzipXArchive(archiveZipPath: string): Promise<string | null> {
const archiveZip = await unzipper.Open.file(archiveZipPath);
if (!this.account) {
return null;
}
const unzippedPath = path.join(getAccountDataPath("X", this.account.username), path.parse(archiveZipPath).name)
await archiveZip.extract({ path: unzippedPath });
return unzippedPath
}

// Delete the unzipped X archive once the build is completed
async deleteUnzippedXArchive(archivePath: string): Promise<void> {
fs.rm(archivePath, { recursive: true, force: true }, err => {
if (err) {
log.error(`XAccountController.deleteUnzippedXArchive: Error occured while deleting unzipped folder: ${err}`);
}
});
}

// Return null on success, and a string (error message) on error
async verifyXArchive(archivePath: string): Promise<string | null> {
const foldersToCheck = [
Expand Down Expand Up @@ -2538,6 +2560,24 @@ export const defineIPCX = () => {
}
});

ipcMain.handle('X:unzipXArchive', async (_, accountID: number, archivePath: string): Promise<string | null> => {
try {
const controller = getXAccountController(accountID);
return await controller.unzipXArchive(archivePath);
} catch (error) {
throw new Error(packageExceptionForReport(error as Error));
}
});

ipcMain.handle('X:deleteUnzippedXArchive', async (_, accountID: number, archivePath: string): Promise<string | null> => {
try {
const controller = getXAccountController(accountID);
return await controller.deleteUnzippedXArchive(archivePath);
} catch (error) {
throw new Error(packageExceptionForReport(error as Error));
}
});

ipcMain.handle('X:verifyXArchive', async (_, accountID: number, archivePath: string): Promise<string | null> => {
try {
const controller = getXAccountController(accountID);
Expand Down Expand Up @@ -2582,4 +2622,4 @@ export const defineIPCX = () => {
throw new Error(packageExceptionForReport(error as Error));
}
});
};
};
23 changes: 23 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,29 @@ async function createWindow() {
}
});

ipcMain.handle('showSelectZIPFileDialog', async (_): Promise<string | null> => {
const dataPath = database.getConfig('dataPath');

const options: Electron.OpenDialogSyncOptions = {
filters: [{ name: 'Archive', extensions: ['zip'] }],
properties: ['openFile'],
};

if (dataPath) {
options.defaultPath = dataPath;
}

try {
const result = dialog.showOpenDialogSync(win, options);
if (result && result.length > 0) {
return result[0];
}
return null;
} catch (error) {
throw new Error(packageExceptionForReport(error as Error));
}
});

ipcMain.handle('showSelectFolderDialog', async (_): Promise<string | null> => {
const dataPath = database.getConfig('dataPath');

Expand Down
11 changes: 10 additions & 1 deletion src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ contextBridge.exposeInMainWorld('electron', {
showQuestion: (message: string, trueText: string, falseText: string): Promise<boolean> => {
return ipcRenderer.invoke('showQuestion', message, trueText, falseText)
},
showSelectZIPFileDialog: (): Promise<string | null> => {
return ipcRenderer.invoke('showSelectZIPFileDialog')
},
showSelectFolderDialog: (): Promise<string | null> => {
return ipcRenderer.invoke('showSelectFolderDialog')
},
Expand Down Expand Up @@ -248,6 +251,12 @@ contextBridge.exposeInMainWorld('electron', {
deleteDMsScrollToBottom: (accountID: number): Promise<void> => {
return ipcRenderer.invoke('X:deleteDMsScrollToBottom', accountID);
},
unzipXArchive: (accountID: number, archivePath: string): Promise<string | null> => {
return ipcRenderer.invoke('X:unzipXArchive', accountID, archivePath);
},
deleteUnzippedXArchive: (accountID: number, archivePath: string): Promise<string | null> => {
return ipcRenderer.invoke('X:deleteUnzippedXArchive', accountID, archivePath);
},
verifyXArchive: (accountID: number, archivePath: string): Promise<string | null> => {
return ipcRenderer.invoke('X:verifyXArchive', accountID, archivePath);
},
Expand All @@ -271,4 +280,4 @@ contextBridge.exposeInMainWorld('electron', {
onPowerMonitorResume: (callback: () => void) => {
ipcRenderer.on('powerMonitor:resume', callback);
},
})
})
5 changes: 4 additions & 1 deletion src/renderer/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ declare global {
showMessage: (message: string) => void;
showError: (message: string) => void;
showQuestion: (message: string, trueText: string, falseText: string) => Promise<boolean>;
showSelectZIPFileDialog: () => Promise<string | null>;
showSelectFolderDialog: () => Promise<string | null>;
openURL: (url: string) => void;
loadFileInWebview: (webContentsId: number, filename: string) => void;
Expand Down Expand Up @@ -107,6 +108,8 @@ declare global {
deleteTweet: (accountID: number, tweetID: string) => Promise<void>;
deleteDMsMarkAllDeleted: (accountID: number) => Promise<void>;
deleteDMsScrollToBottom: (accountID: number) => Promise<void>;
unzipXArchive: (accountID: number, archivePath: string) => Promise<string | null>;
deleteUnzippedXArchive: (accountID: number, archivePath: string) => Promise<string | null>;
verifyXArchive: (accountID: number, archivePath: string) => Promise<string | null>;
importXArchive: (accountID: number, archivePath: string, dataType: string) => Promise<XImportArchiveResponse>;
getCookie: (accountID: number, name: string) => Promise<string | null>;
Expand All @@ -123,4 +126,4 @@ const emitter = mitt();
const app = createApp(App);

app.config.globalProperties.emitter = emitter;
app.mount('#app');
app.mount('#app');
31 changes: 24 additions & 7 deletions src/renderer/src/views/x/XWizardImportingPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,32 @@ const startClicked = async () => {
errorMessages.value = [];
importStarted.value = true;

// Unarchive the zip
statusValidating.value = ImportStatus.Active;
const unzippedPath: string | null = await window.electron.X.unzipXArchive(props.model.account.id, importFromArchivePath.value);
if (unzippedPath === null) {
statusValidating.value = ImportStatus.Failed;
errorMessages.value.push(unzippedPath);
importFailed.value = true;
return;
}
statusValidating.value = ImportStatus.Finished;

// Verify that the archive is valid
statusValidating.value = ImportStatus.Active;
const verifyResp: string | null = await window.electron.X.verifyXArchive(props.model.account.id, importFromArchivePath.value);
const verifyResp: string | null = await window.electron.X.verifyXArchive(props.model.account.id, unzippedPath);
if (verifyResp !== null) {
statusValidating.value = ImportStatus.Failed;
errorMessages.value.push(verifyResp);
importFailed.value = true;
await window.electron.X.deleteUnzippedXArchive(props.model.account.id, unzippedPath);
return;
}
statusValidating.value = ImportStatus.Finished;

// Import tweets
statusImportingTweets.value = ImportStatus.Active;
const tweetsResp: XImportArchiveResponse = await window.electron.X.importXArchive(props.model.account.id, importFromArchivePath.value, 'tweets');
const tweetsResp: XImportArchiveResponse = await window.electron.X.importXArchive(props.model.account.id, unzippedPath, 'tweets');
tweetCountString.value = createCountString(tweetsResp.importCount, tweetsResp.skipCount);
if (tweetsResp.status == 'error') {
statusImportingTweets.value = ImportStatus.Failed;
Expand All @@ -80,7 +92,7 @@ const startClicked = async () => {

// Import likes
statusImportingLikes.value = ImportStatus.Active;
const likesResp: XImportArchiveResponse = await window.electron.X.importXArchive(props.model.account.id, importFromArchivePath.value, 'likes');
const likesResp: XImportArchiveResponse = await window.electron.X.importXArchive(props.model.account.id, unzippedPath, 'likes');
likeCountString.value = createCountString(likesResp.importCount, likesResp.skipCount);
if (likesResp.status == 'error') {
statusImportingLikes.value = ImportStatus.Failed;
Expand All @@ -99,20 +111,25 @@ const startClicked = async () => {
statusBuildCydArchive.value = ImportStatus.Failed;
errorMessages.value.push(`${e}`);
importFailed.value = true;
await window.electron.X.deleteUnzippedXArchive(props.model.account.id, unzippedPath);
return;
}
emitter.emit(`x-update-archive-info-${props.model.account.id}`);
statusBuildCydArchive.value = ImportStatus.Finished;

// Delete the unarchived folder whether it's success or fail
await window.electron.X.deleteUnzippedXArchive(props.model.account.id, unzippedPath);

// Success
if (!importFailed.value) {
await window.electron.X.setConfig(props.model.account.id, 'lastFinishedJob_importArchive', new Date().toISOString());
importFinished.value = true;
}

};

const importFromArchiveBrowserClicked = async () => {
const path = await window.electron.showSelectFolderDialog();
const path = await window.electron.showSelectZIPFileDialog();
if (path) {
importFromArchivePath.value = path;
}
Expand Down Expand Up @@ -162,7 +179,7 @@ const iconFromStatus = (status: ImportStatus) => {
</h2>
<p class="text-muted">
<template v-if="!importStarted">
Unzip your archive and choose the folder that it's in.
Browse for the ZIP file of the X archive you downloaded.
</template>
<template v-else>
Importing your archive...
Expand Down Expand Up @@ -199,7 +216,7 @@ const iconFromStatus = (status: ImportStatus) => {
<i v-else>
<RunningIcon />
</i>
Validating X archive
Unzipping and validating X archive
</li>
<li :class="statusImportingTweets == ImportStatus.Pending ? 'text-muted' : ''">
<i v-if="statusImportingTweets != ImportStatus.Active"
Expand Down Expand Up @@ -288,4 +305,4 @@ ul.import-status li {
ul.import-status li i {
margin-right: 0.5rem;
}
</style>
</style>
Loading