Skip to content

Commit

Permalink
fix: EPUB / publication export save-as OS file selector (PR #2036 Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
panaC authored Nov 16, 2023
1 parent 924fb53 commit a7c9f67
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
20 changes: 7 additions & 13 deletions src/main/redux/sagas/api/publication/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,28 @@
// ==LICENSE-END==

import { dialog } from "electron";
import { promises as fsp } from "fs";
import * as path from "path";
import { call as callTyped } from "typed-redux-saga/macro";
import { PublicationView } from "readium-desktop/common/views/publication";
import { diMainGet, getLibraryWindowFromDi } from "readium-desktop/main/di";

export function* exportPublication(publicationView: PublicationView) {

const libraryAppWindow = yield* callTyped(() => getLibraryWindowFromDi());
const publicationStorage = diMainGet("publication-storage");

const defaultFilename = publicationStorage.getPublicationFilename(publicationView);
// Open a dialog to select a folder then copy the publication in it
const res = yield* callTyped(() => dialog.showOpenDialog(
const res = yield* callTyped(() => dialog.showSaveDialog(
libraryAppWindow ? libraryAppWindow : undefined,
{
properties: ["openDirectory"],
defaultPath: defaultFilename,
properties: ["createDirectory"],
},
));

if (!res.canceled) {
if (res.filePaths && res.filePaths.length > 0) {
let destinationPath = res.filePaths[0];
// If the selected path is a file then choose the directory containing this file
const stat = yield* callTyped(() => fsp.stat(destinationPath));
if (stat?.isFile()) {
destinationPath = path.dirname(destinationPath);
}
const publicationStorage = diMainGet("publication-storage");
publicationStorage.copyPublicationToPath(publicationView, destinationPath);
if (res.filePath) {
publicationStorage.copyPublicationToPath(publicationView, res.filePath);
}
}
}
14 changes: 11 additions & 3 deletions src/main/storage/publication-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,19 @@ export class PublicationStorage {
throw new Error(`getPublicationEpubPath() FAIL ${identifier} (cannot find book.epub|audiobook|etc.)`);
}

public copyPublicationToPath(publicationView: PublicationView, destinationPath: string) {
public getPublicationFilename(publicationView: PublicationView) {
const publicationPath = this.getPublicationEpubPath(publicationView.identifier);
const extension = path.extname(publicationPath);
const newFilePath = `${destinationPath}/${slugify(publicationView.documentTitle)}${extension}`;
fs.copyFile(publicationPath, newFilePath, async (err) => {
const filename = `${slugify(publicationView.documentTitle)}${extension}`;
return filename;
}

public copyPublicationToPath(publicationView: PublicationView, filePath: string) {
if (!filePath) {
throw new Error("no filePath !");
}
const publicationPath = this.getPublicationEpubPath(publicationView.identifier);
fs.copyFile(publicationPath, filePath, async (err) => {
if (err) {
await dialog.showMessageBox({
type: "error",
Expand Down
2 changes: 1 addition & 1 deletion src/resources/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"continueReadingPdf": "Resume PDF",
"lastAdditions": "Recently added"
},
"export": "Export",
"export": "Save as",
"format": "Format",
"lang": "Language",
"lastRead": "Last Read",
Expand Down
2 changes: 1 addition & 1 deletion src/resources/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"continueReadingPdf": "",
"lastAdditions": "Récemment ajouté"
},
"export": "Exporter",
"export": "Sauvegarder sous",
"format": "",
"lang": "Langue",
"lastRead": "Dernière lecture",
Expand Down

0 comments on commit a7c9f67

Please sign in to comment.