浏览代码

Уменьшена длина имени файла при скачивании (#7)

Book Pauk 2 年之前
父节点
当前提交
ed916e1b28
共有 1 个文件被更改,包括 12 次插入5 次删除
  1. 12 5
      server/core/WebWorker.js

+ 12 - 5
server/core/WebWorker.js

@@ -433,20 +433,27 @@ class WebWorker {
             const db = this.db;
             let link = '';
 
-            //найдем bookPath и downFileName
+            //найдем downFileName и bookPath
             let rows = await db.select({table: 'book', where: `@@hash('_uid', ${db.esc(bookUid)})`});
             if (!rows.length)
                 throw new Error('404 Файл не найден');
 
             const book = rows[0];
             let downFileName = book.file;
-            const author = book.author.split(',');
-            const at = [author[0], book.title];
-            downFileName = utils.makeValidFileNameOrEmpty(at.filter(r => r).join(' - '))
+            const authors = book.author.split(',');
+            let author = authors[0];
+            author = author.split(' ').filter(r => r.trim());
+            for (let i = 1; i < author.length; i++)
+                author[i] = `${(i === 1 ? ' ' : '')}${author[i][0]}.`;
+            if (authors.length > 1)
+                author.push(' и др.');
+
+            const at = [author.join(''), (book.title ? `_${book.title}` : '')];
+            downFileName = utils.makeValidFileNameOrEmpty(at.filter(r => r).join(''))
                 || utils.makeValidFileNameOrEmpty(at[0])
                 || utils.makeValidFileNameOrEmpty(at[1])
                 || downFileName;
-            downFileName = downFileName.substring(0, 100);
+            downFileName = downFileName.substring(0, 50);
 
             const ext = `.${book.ext}`;
             if (downFileName.substring(downFileName.length - ext.length) != ext)