Skip to content

Commit

Permalink
Sophosia-prep (#73)
Browse files Browse the repository at this point in the history
* export db button added

* able to export as sophosia-ready folder

* remove problematic citekey connectors

* fixed a test

* change project.path to project.pdf

* fix export issue
  • Loading branch information
HuntFeng authored Dec 8, 2023
1 parent 6839a2c commit cd107c2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
29 changes: 22 additions & 7 deletions src/backend/export/sophosiadb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,15 @@ export async function exportDB(
for (const folder of folders) {
delete (folder as { _rev?: string })._rev;
const oldFolderId = folder._id;
folder._id = "SF" + nanoid();
folder._id = "SF" + (folder._id === "library" ? "library" : nanoid());
folder.icon = folder._id === "SFlibrary" ? "mdi-bookshelf" : "mdi-folder";
old2new.set(oldFolderId, folder._id);
}

// update folderIds in the children
for (const folder of folders) {
for (const [ind, oldId] of folder.children.entries())
folder.children[ind] = old2new.get(oldId as string) as string;
jsonPath = path.join(folderPaths.folder, folder._id + ".json");
fs.writeFileSync(jsonPath, JSON.stringify(folder));

Expand All @@ -133,6 +140,15 @@ export async function exportDB(
const oldProjectFolder = path.join(oldStoragePath, oldProjectId);
const projectFolder = path.join(storagePath, project._id);
fs.mkdirSync(projectFolder);
// process the pdf, image files and notes
// copy the pdf file into new folder regardless the pdf is linked or in the project folder
if (project.path) {
const oldPDFPath = project.path;
const newPDFPath = path.join(projectFolder, path.basename(oldPDFPath));
fs.copyFileSync(oldPDFPath, newPDFPath);
delete project.path;
project.pdf = path.basename(newPDFPath);
}
const entries = fs.readdirSync(oldProjectFolder);
for (const entry of entries) {
if (entry === "img") {
Expand All @@ -147,10 +163,7 @@ export async function exportDB(
fs.copyFileSync(oldPath, newPath);
}
} else if (path.extname(entry) === ".pdf") {
const oldFilePath = path.join(oldProjectFolder, entry);
const newFilePath = path.join(projectFolder, entry);
fs.copyFileSync(oldFilePath, newFilePath);
project.path = newFilePath;
// no need to do anything
} else {
// notes: md, and excalidraw
const ext = path.extname(entry); // ext has the .
Expand Down Expand Up @@ -219,7 +232,7 @@ ${t("note-is-auto-manged")}`;
delete (pdfState as { _rev?: string })._rev;
pdfState._id = "SS" + old2new.get(pdfState.projectId)!.slice(2);

Check warning on line 233 in src/backend/export/sophosiadb.ts

View workflow job for this annotation

GitHub Actions / release (macos-latest)

Forbidden non-null assertion

Check warning on line 233 in src/backend/export/sophosiadb.ts

View workflow job for this annotation

GitHub Actions / release (ubuntu-latest)

Forbidden non-null assertion

Check warning on line 233 in src/backend/export/sophosiadb.ts

View workflow job for this annotation

GitHub Actions / release (windows-latest)

Forbidden non-null assertion
jsonPath = path.join(
folderPaths.project,
folderPaths.pdfState,
old2new.get(pdfState.projectId) + ".json"
);
fs.writeFileSync(jsonPath, JSON.stringify(pdfState));
Expand All @@ -231,7 +244,9 @@ ${t("note-is-auto-manged")}`;
// pdfAnnotation
for (const annotData of annotDatas) {
delete (annotData as { _rev?: string })._rev;
annotData._id = "SA" + nanoid;
annotData._id = "SA" + nanoid();
annotData.projectId = old2new.get(annotData.projectId) as string;
jsonPath = path.join(folderPaths.pdfAnnotation, annotData._id + ".json");
fs.writeFileSync(jsonPath, JSON.stringify(annotData));

currentStep++;
Expand Down
33 changes: 8 additions & 25 deletions src/components/settings/GeneralTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
readonly
input-style="cursor: pointer; font-size: 1rem"
v-model="stateStore.settings.storagePath"
@click="showFolderPicker(true)"
@click="showFolderPicker()"
>
<template v-slot:before>
<div style="font-size: 1rem">{{ $t("storage-path") }}</div>
Expand All @@ -123,7 +123,6 @@
color="primary"
:ripple="false"
:label="$t('export-database')"
:disable="disableExportBtn"
@click="() => exportAsSophosiaDB()"
>
</q-btn>
Expand All @@ -138,20 +137,6 @@
>{{ " " + $t("what-is-sophosia") }}</a
>
</p>
<q-input
dense
outlined
square
readonly
input-style="cursor: pointer; font-size: 1rem"
v-model="newStoragePath"
:placeholder="$t('select-new-path')"
@click="showFolderPicker(false)"
>
<template v-slot:before>
<div style="font-size: 1rem">{{ $t("new-storage-path") }}</div>
</template>
</q-input>
</q-card-section>
</q-card>

Expand Down Expand Up @@ -355,15 +340,11 @@ const citeKeyConnector = ref(
* so that the newStoragePath select can be used in exportDB section
* @param changePath
*/
async function showFolderPicker(changePath: boolean) {
async function showFolderPicker() {
let result = window.fileBrowser.showFolderPicker();
if (result !== undefined && !!result[0]) {
let storagePath = result[0]; // do not update texts in label yet
if (changePath) await changeStoragePath(storagePath);
else {
newStoragePath.value = storagePath;
disableExportBtn.value = false;
}
await changeStoragePath(storagePath);
}
}
Expand Down Expand Up @@ -426,12 +407,14 @@ async function moveFiles(oldPath: string, newPath: string) {
}
async function exportAsSophosiaDB() {
if (!newStoragePath.value) return;
let result = window.fileBrowser.showFolderPicker();
if (!result || !result[0]) return;
const newStoragePath = result[0]; // do not update texts in label yet
if (!newStoragePath) return;
showProgressDialog.value = true;
await exportDB(newStoragePath.value, (prog) => {
await exportDB(newStoragePath, (prog) => {
progress.value = prog;
});
disableExportBtn.value = true;
}
function citeKeyExample(meta: Meta) {
Expand Down

0 comments on commit cd107c2

Please sign in to comment.