Skip to content

Commit

Permalink
Merge pull request #92 from GeoffreyChen777/electron-dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
GeoffreyChen777 authored Jun 24, 2022
2 parents 920b12d + 3928b41 commit 6a227d8
Show file tree
Hide file tree
Showing 26 changed files with 628 additions and 67 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"electron-updater": "^5.0.1",
"fast-xml-parser": "^4.0.6",
"got": "11.8.3",
"hpagent": "^1.0.0",
"keytar": "^7.9.0",
"node-html-parser": "^5.3.3",
"realm": "^10.13.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ async function createWindow() {
title: "Main window",
width: 1440,
height: 860,
minWidth: 1440,
minHeight: 860,
minWidth: 800,
minHeight: 600,
useContentSize: true,
webPreferences: {
preload: join(__dirname, "../preload/index.cjs"),
Expand Down
18 changes: 17 additions & 1 deletion packages/preload/interactors/entity-interactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,22 @@ export class EntityInteractor {
);
}

async addWholeFolder(folder: string) {
const files = await this.fileRepository.listPDFs(folder);
const PDFfiles = files.filter((file) => path.extname(file) === ".pdf");

this.add(PDFfiles);
}

async addFromZoteroCSV(csvFile: string) {
if (path.extname(csvFile) === ".csv") {
const paperEntityDrafts = await this.fileRepository.parseZoteroCSV(
csvFile
);
this.update(JSON.stringify(paperEntityDrafts));
}
}

// ============================================================
// Delete
async delete(ids: string[]) {
Expand Down Expand Up @@ -251,7 +267,7 @@ export class EntityInteractor {
const updatePromise = async (entityDrafts: PaperEntityDraft[]) => {
const movedEntityDrafts = await Promise.all(
entityDrafts.map((entityDraft: PaperEntityDraft) =>
this.fileRepository.move(entityDraft)
this.fileRepository.move(entityDraft, true)
)
);

Expand Down
4 changes: 2 additions & 2 deletions packages/preload/models/PaperEntityDraft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ export class PaperEntityDraft {
return entity;
}

setValue(key: string, value: unknown) {
if (value && value !== "undefined") {
setValue(key: string, value: unknown, allowEmpty = false) {
if ((value || allowEmpty) && value !== "undefined") {
this[key] = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ export class BibExporter extends Exporter {

for (const entity of entityDrafts) {
let citeKey = "";
const nameArray = entity.authors.split(", ")[0].split(" ");
let nameArray;
if (entity.authors.includes(";")) {
nameArray = entity.authors.split(";")[0].split(" ");
} else {
nameArray = entity.authors.split(", ")[0].split(" ");
}
const lastName = nameArray[nameArray.length - 1];
citeKey += lastName.toLowerCase();
citeKey += entity.pubTime;
Expand All @@ -36,7 +41,7 @@ export class BibExporter extends Exporter {

const pubDetails = {
volume: entity.volume,
number: entity.number,
issue: entity.number,
pages: entity.pages,
publisher: entity.publisher,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export interface FileBackend {

check(): void;
access(url: string, download: boolean): Promise<string>;
move(entity: PaperEntityDraft): Promise<PaperEntityDraft | null>;
move(
entity: PaperEntityDraft,
fourceDelete: boolean
): Promise<PaperEntityDraft | null>;
remove(entity: PaperEntityDraft): Promise<boolean>;
removeFile(url: string): Promise<boolean>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,23 @@ export class LocalFileBackend implements FileBackend {
this.preference.get("appLibFolder") as string,
"file://"
);
const pathStat = await fsPromise.lstat(fileURL.replace("file://", ""));
if (existsSync(fileURL.replace("file://", "")) && pathStat.isFile()) {
return Promise.resolve(fileURL);
if (existsSync(fileURL.replace("file://", ""))) {
const pathStat = await fsPromise.lstat(fileURL.replace("file://", ""));
if (pathStat.isFile()) {
return Promise.resolve(fileURL);
} else {
return Promise.resolve("");
}
} else {
return Promise.resolve("");
}
}

async _move(sourceURL: string, targetURL: string): Promise<boolean> {
async _move(
sourceURL: string,
targetURL: string,
forceDelete: boolean = false
): Promise<boolean> {
const _sourceURL = sourceURL.replace("file://", "");
const _targetURL = targetURL.replace("file://", "");
const stat = await fsPromise.lstat(_sourceURL);
Expand All @@ -50,7 +58,7 @@ export class LocalFileBackend implements FileBackend {
try {
await fsPromise.copyFile(_sourceURL, _targetURL);
if (
(this.preference.get("deleteSourceFile") as boolean) &&
((this.preference.get("deleteSourceFile") as boolean) || forceDelete) &&
_sourceURL !== _targetURL
) {
await fsPromise.unlink(sourceURL);
Expand All @@ -65,11 +73,34 @@ export class LocalFileBackend implements FileBackend {
}
}

async move(entity: PaperEntityDraft): Promise<PaperEntityDraft | null> {
const targetFileName =
entity.title.replace(/[^a-zA-Z0-9 ]/g, "").replace(/\s/g, "_") +
"_" +
entity._id.toString();
async move(
entity: PaperEntityDraft,
forceDelete: boolean = false
): Promise<PaperEntityDraft | null> {
let title = entity.title.replace(/[^a-zA-Z0-9 ]/g, "").replace(/\s/g, "_");
let id = entity._id.toString();
if (this.preference.get("renamingFormat") === "short") {
title = title
.split("_")
.map((word: string) => {
if (word) {
return word.slice(0, 1);
} else {
return "";
}
})
.filter((c: string) => c && c === c.toUpperCase())
.join("");
} else if (this.preference.get("renamingFormat") === "authortitle") {
let author = entity.authors.split(",")[0];
if (author !== entity.authors) {
author = `${author} et al`;
}
title = `${author} - ${title.slice(0, 20)}`;
id = id.slice(-5, -1);
}

const targetFileName = title + "_" + id;

// 1. Move main file.
const sourceMainURL = constructFileURL(
Expand All @@ -84,7 +115,11 @@ export class LocalFileBackend implements FileBackend {
false,
this.preference.get("appLibFolder") as string
);
const mainSuccess = await this._move(sourceMainURL, targetMainURL);
const mainSuccess = await this._move(
sourceMainURL,
targetMainURL,
forceDelete
);
if (mainSuccess) {
entity.mainURL = path.basename(targetMainURL);
} else {
Expand All @@ -106,7 +141,11 @@ export class LocalFileBackend implements FileBackend {
sourceSupURL: string,
targetSupURL: string
) => {
const supSuccess = await this._move(sourceSupURL, targetSupURL);
const supSuccess = await this._move(
sourceSupURL,
targetSupURL,
forceDelete
);
if (supSuccess) {
return path.basename(targetSupURL);
} else {
Expand Down
Loading

0 comments on commit 6a227d8

Please sign in to comment.