Перенос на сервер работы с именами файлов при скачивании

This commit is contained in:
Book Pauk
2022-11-06 18:03:03 +07:00
parent d9f1912ea2
commit 55239159ba
6 changed files with 69 additions and 106 deletions

View File

@@ -231,12 +231,12 @@ class Api {
return await this.request({action: 'get-genre-tree'});
}
async getBookLink(params) {
return await this.request(Object.assign({action: 'get-book-link'}, params), 120);
async getBookLink(bookId) {
return await this.request({action: 'get-book-link', bookId}, 120);
}
async getBookInfo(params) {
return await this.request(Object.assign({action: 'get-book-info'}, params), 120);
async getBookInfo(bookId) {
return await this.request({action: 'get-book-info', bookId}, 120);
}
async getConfig() {

View File

@@ -129,31 +129,8 @@ export default class BaseList {
})();
try {
const makeValidFilenameOrEmpty = (s) => {
try {
return utils.makeValidFilename(s);
} catch(e) {
return '';
}
};
//имя файла
let downFileName = 'default-name';
const author = book.author.split(',');
const at = [author[0], book.title];
downFileName = makeValidFilenameOrEmpty(at.filter(r => r).join(' - '))
|| makeValidFilenameOrEmpty(at[0])
|| makeValidFilenameOrEmpty(at[1])
|| downFileName;
downFileName = downFileName.substring(0, 100);
const ext = `.${book.ext}`;
if (downFileName.substring(downFileName.length - ext.length) != ext)
downFileName += ext;
const bookPath = `${book.folder}/${book.file}${ext}`;
//подготовка
const response = await this.api.getBookLink({bookPath, downFileName});
const response = await this.api.getBookLink(book.id);
const link = response.link;
const href = `${window.location.origin}${link}`;
@@ -162,7 +139,7 @@ export default class BaseList {
//скачивание
const d = this.$refs.download;
d.href = href;
d.download = downFileName;
d.download = response.downFileName;
d.click();
} else if (action == 'copyLink') {
@@ -187,7 +164,7 @@ export default class BaseList {
}
} else if (action == 'bookInfo') {
//информация о книге
const response = await this.api.getBookInfo({bookPath, downFileName});
const response = await this.api.getBookInfo(book.id);
this.$emit('listEvent', {action: 'bookInfo', data: response.bookInfo});
}
} catch(e) {

View File

@@ -87,18 +87,6 @@ export async function copyTextToClipboard(text) {
return result;
}
export function makeValidFilename(filename, repl = '_') {
let f = filename.replace(/[\x00\\/:*"<>|]/g, repl); // eslint-disable-line no-control-regex
f = f.trim();
while (f.length && (f[f.length - 1] == '.' || f[f.length - 1] == '_')) {
f = f.substring(0, f.length - 1);
}
if (f)
return f;
else
throw new Error('Invalid filename');
}
/*
export function formatDate(d, format = 'normal') {
switch (format) {