Skip to content
Merged
Changes from 1 commit
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
108 changes: 58 additions & 50 deletions src/main/services/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ export class CatalogService {
public async importFile(filePath: string, isLcpFile?: boolean): Promise<PublicationDocument> {
const ext = path.extname(filePath);

debug("Import File - START");
if (ext === ".lcpl" || (ext === ".part" && isLcpFile)) {
return this.importLcplFile(filePath);
} else if (/\.epub[3]?$/.test(ext) || (ext === ".part" && !isLcpFile)) {
return this.importEpubFile(filePath);
}
debug("Import File - END");

return null;
}
Expand Down Expand Up @@ -270,60 +272,66 @@ export class CatalogService {

private async importEpubFile(filePath: string): Promise<PublicationDocument> {
debug("Parse publication - START", filePath);
const parsedPublication: Epub = await EpubParsePromise(filePath);
debug("Parse publication - END", filePath);

// FIXME: Title could be an array instead of a simple string
// Store publication in db
const jsonParsedPublication = TAJSON.serialize(parsedPublication);
const b64ParsedPublication = Buffer
.from(JSON.stringify(jsonParsedPublication))
.toString("base64");

const pubDocument = {
identifier: uuid.v4(),
resources: {
filePublication: b64ParsedPublication,
opdsPublication: null,
},
title: convertMultiLangStringToString(parsedPublication.Metadata.Title),
tags: [],
files: [],
coverFile: null,
customCover: null,
} as PublicationDocument;

// Store publication on filesystem
debug("[START] Store publication on filesystem", filePath);
const files = await this.publicationStorage.storePublication(
pubDocument.identifier, filePath,
);
debug("[END] Store publication on filesystem - END", filePath);

// Add extracted files to document
try {
const parsedPublication: Epub = await EpubParsePromise(filePath);
debug("Parse publication - END", filePath);

// FIXME: Title could be an array instead of a simple string
// Store publication in db
const jsonParsedPublication = TAJSON.serialize(parsedPublication);
const b64ParsedPublication = Buffer
.from(JSON.stringify(jsonParsedPublication))
.toString("base64");

const pubDocument = {
identifier: uuid.v4(),
resources: {
filePublication: b64ParsedPublication,
opdsPublication: null,
},
title: convertMultiLangStringToString(parsedPublication.Metadata.Title),
tags: [],
files: [],
coverFile: null,
customCover: null,
} as PublicationDocument;

// Store publication on filesystem
debug("[START] Store publication on filesystem", filePath);
const files = await this.publicationStorage.storePublication(
pubDocument.identifier, filePath,
);
debug("[END] Store publication on filesystem - END", filePath);

// Add extracted files to document

for (const file of files) {
if (file.contentType.startsWith("image")) {
pubDocument.coverFile = file;
} else {
pubDocument.files.push(file);
}
}

for (const file of files) {
if (file.contentType.startsWith("image")) {
pubDocument.coverFile = file;
} else {
pubDocument.files.push(file);
if (pubDocument.coverFile === null) {
debug("No cover found, generate custom one", filePath);
// No cover file found
// Generate a random custom cover
pubDocument.customCover = RandomCustomCovers[
Math.floor(Math.random() * RandomCustomCovers.length)
];
}
}

if (pubDocument.coverFile === null) {
debug("No cover found, generate custom one", filePath);
// No cover file found
// Generate a random custom cover
pubDocument.customCover = RandomCustomCovers[
Math.floor(Math.random() * RandomCustomCovers.length)
];
}
debug("[START] Store publication in database", filePath);
const newPubDocument = await this.publicationRepository.save(pubDocument);
debug("[END] Store publication in database", filePath);

debug("[START] Store publication in database", filePath);
const newPubDocument = await this.publicationRepository.save(pubDocument);
debug("[END] Store publication in database", filePath);
debug("Publication imported", filePath);
return newPubDocument;
} catch (error) {
debug("Import EPUB File - ERROR", error);
}

debug("Publication imported", filePath);
return newPubDocument;
return undefined;
Comment thread
danielweck marked this conversation as resolved.
Outdated
}
}