فهرست منبع

Добавлена проверка наличия файла на сервере перед скачиванием fb2

Book Pauk 5 سال پیش
والد
کامیت
eebf17c42c
2فایلهای تغییر یافته به همراه22 افزوده شده و 4 حذف شده
  1. 5 1
      client/api/reader.js
  2. 17 3
      client/components/Reader/RecentBooksPage/RecentBooksPage.vue

+ 5 - 1
client/api/reader.js

@@ -56,7 +56,11 @@ class Reader {
         }
     }
 
-    async loadCachedBook(url, callback){
+    async checkUrl(url) {
+        return await axios.head(url, {headers: {'Cache-Control': 'no-cache'}});
+    }
+
+    async loadCachedBook(url, callback) {
         const response = await axios.head(url);
 
         let estSize = 1000000;

+ 17 - 3
client/components/Reader/RecentBooksPage/RecentBooksPage.vue

@@ -5,6 +5,7 @@
             <span v-show="loading"><i class="el-icon-loading" style="font-size: 25px"></i> <span style="position: relative; top: -4px">Список загружается</span></span>
         </template>
 
+        <a ref="download" style='display: none;'></a>
         <el-table
             :data="tableData"
             style="width: 570px"
@@ -72,7 +73,7 @@
                     >
                     <template slot-scope="scope">
                         <a v-show="isUrl(scope.row.url)" :href="scope.row.url" target="_blank">Оригинал</a><br>
-                        <a :href="scope.row.path" :download="getFileNameFromPath(scope.row.path)">Скачать FB2</a>
+                        <a :href="scope.row.path" @click.prevent="downloadBook(scope.row.path)">Скачать FB2</a>
                     </template>
                 </el-table-column>
 
@@ -104,6 +105,7 @@ import _ from 'lodash';
 import * as utils from '../../../share/utils';
 import Window from '../../share/Window.vue';
 import bookManager from '../share/bookManager';
+import readerApi from '../../../api/reader';
 
 export default @Component({
     components: {
@@ -268,8 +270,20 @@ class RecentBooksPage extends Vue {
         return result;
     }
 
-    getFileNameFromPath(fb2Path) {
-        return path.basename(fb2Path).substr(0, 10) + '.fb2';
+    async downloadBook(fb2path) {
+        try {
+            await readerApi.checkUrl(fb2path);
+
+            const d = this.$refs.download;
+            d.href = fb2path;
+            d.download = path.basename(fb2path).substr(0, 10) + '.fb2';
+            d.click();
+        } catch (e) {
+            let errMes = e.message;
+            if (errMes.indexOf('404') >= 0)
+                errMes = 'Файл не найден на сервере (возможно был удален как устаревший)';
+            this.$alert(errMes, 'Ошибка', {type: 'error'});
+        }
     }
 
     openOriginal(url) {