diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php index c92425182..8752b478b 100755 --- a/app/Http/Controllers/IndexController.php +++ b/app/Http/Controllers/IndexController.php @@ -7,9 +7,12 @@ use App\Tasks\AutoArchivedTask; use App\Tasks\DeleteTmpTask; use App\Tasks\OverdueRemindEmailTask; use Arr; +use Cache; +use Carbon\Carbon; use Hhxsv5\LaravelS\Swoole\Task\Task; use Redirect; use Request; +use Route; /** @@ -74,6 +77,36 @@ class IndexController extends InvokeController return $array; } + /** + * 下载链接 + * @return array|\Illuminate\Http\RedirectResponse|string + */ + public function download() + { + $id = Request::input('id'); + if ($id) { + $url = Cache::get("Download::" . $id); + if ($url) { + sleep(1); + return Redirect::to($url, 301); + } + } + // + $action = Route::input('action'); + if ($action) { + $url = Base::fillUrl('download?id=' . $action); + return ""; + } + // + $key = Request::input('key'); + if ($key) { + $id = md5($key); + Cache::put("Download::" . $id, $key, Carbon::now()->addDay()); + return Redirect::to(Base::fillUrl('download/' . $id), 301); + } + return Base::ajaxError("Timeout", [], 0, 403); + } + /** * 接口文档 * @return \Illuminate\Http\RedirectResponse diff --git a/electron/electron.js b/electron/electron.js index 6d3e68441..878b03bc4 100644 --- a/electron/electron.js +++ b/electron/electron.js @@ -1,5 +1,4 @@ const fs = require('fs') -const fse = require('fs-extra') const os = require("os"); const path = require('path') const {app, BrowserWindow, ipcMain, dialog, clipboard, nativeImage, shell} = require('electron') @@ -16,53 +15,12 @@ let mainWindow = null, subWindow = [], willQuitApp = false, devloadUrl = "", - devloadCachePath = path.resolve(__dirname, ".devload"), - downloadList = [], - downloadCacheFile = path.join(app.getPath('cache'), config.name, '.downloadCache'); + devloadCachePath = path.resolve(__dirname, ".devload"); if (fs.existsSync(devloadCachePath)) { devloadUrl = fs.readFileSync(devloadCachePath, 'utf8') } -if (fs.existsSync(downloadCacheFile)) { - downloadList = utils.jsonParse(fs.readFileSync(downloadCacheFile, 'utf8'), []) -} else { - fse.ensureDirSync(path.join(app.getPath('cache'), config.name)) -} - -function downloadUpdate(item) { - const chain = item.getURLChain() - if (chain.length == 0) { - return - } - let currentState = item.getState() - if (currentState == "progressing" && item.isPaused()) { - currentState = "paused" - } - // - const downloadItem = downloadList.find(item => item.url == chain[0]) - if (downloadItem && downloadItem.state != currentState) { - downloadItem.state = currentState; - downloadItem.result = { - url: item.getURL(), - name: item.getFilename(), - savePath: item.getSavePath(), - mimeType: item.getMimeType(), - totalBytes: item.getTotalBytes(), - chain, - }; - fs.writeFileSync(downloadCacheFile, utils.jsonStringify(downloadList), 'utf8'); - // - if (currentState == 'completed') { - mainWindow.webContents.send("downloadDone", downloadItem) - log.info("下载完成", downloadItem) - } else { - mainWindow.webContents.send("downloadUpdate", downloadItem) - log.info("下载更新", downloadItem) - } - } -} - function createMainWindow() { mainWindow = new BrowserWindow({ width: 1280, @@ -100,16 +58,6 @@ function createMainWindow() { utils.onBeforeUnload(event, app) } }) - - mainWindow.webContents.session.on('will-download', (event, item) => { - item.setSavePath(path.join(app.getPath('cache'), config.name, item.getFilename())); - item.on('updated', () => { - downloadUpdate(item) - }) - item.on('done', () => { - downloadUpdate(item) - }) - }) } /** @@ -202,40 +150,6 @@ app.on('before-quit', () => { willQuitApp = true }) -/** - * 下载文件 - * @param args {url} - */ -ipcMain.on('downloadFile', (event, args) => { - event.returnValue = "ok" - // - let appendJson = {state: "progressing", startTime: utils.Time()} - let downloadItem = downloadList.find(({url}) => url == args.url) - if (downloadItem) { - switch (downloadItem.state) { - case "completed": - if (fs.existsSync(downloadItem.result.savePath)) { // 下载完成,文件存在 - log.info("下载已完成", downloadItem) - mainWindow.webContents.send("downloadDone", downloadItem) - return - } - break; - case "progressing": - if (downloadItem.startTime + 480 > utils.Time()) { // 下载中,未超时(超时时间8分钟) - log.info("下载已存在", downloadItem) - return; - } - break; - } - downloadItem = Object.assign(downloadItem, appendJson) - } else { - downloadList.push(downloadItem = Object.assign(args, appendJson)) - } - fs.writeFileSync(downloadCacheFile, utils.jsonStringify(downloadList), 'utf8'); - mainWindow.webContents.downloadURL(downloadItem.url); - log.info("下载开始", downloadItem) -}) - /** * 打开文件 * @param args {path} diff --git a/resources/assets/js/functions/common.js b/resources/assets/js/functions/common.js index 02c8c524c..31bd12863 100755 --- a/resources/assets/js/functions/common.js +++ b/resources/assets/js/functions/common.js @@ -134,6 +134,22 @@ return (string.substring(string.length - find.length) === find); }, + /** + * 删除右边字符串 + * @param string + * @param find + * @param lower + * @returns {string} + */ + rightDelete(string, find, lower = false) { + string += ""; + find += ""; + if (this.rightExists(string, find, lower)) { + string = string.substring(0, string.length - find.length) + } + return string ? string : ''; + }, + /** * 取字符串中间 * @param string @@ -683,7 +699,7 @@ url+= '&' + key + '=' + params[key]; } } - return url.replace("?&", "?"); + return this.rightDelete(url.replace("?&", "?"), '?'); }, /** diff --git a/resources/assets/js/functions/web.js b/resources/assets/js/functions/web.js index d03e5f9ff..8395c6003 100755 --- a/resources/assets/js/functions/web.js +++ b/resources/assets/js/functions/web.js @@ -359,25 +359,6 @@ return this.dialogTags(dialog).find(({color}) => color == 'success'); }, - /** - * 下载文件 - * @param url - */ - downFile(url) { - if (!url) { - return - } - if ($A.Electron) { - $A.Electron.request({action: 'openExternal', url}, () => { - // 成功 - }, () => { - // 失败 - }); - } else { - window.open(url) - } - }, - /** * 返回对话未读数量 * @param dialog diff --git a/resources/assets/js/pages/manage.vue b/resources/assets/js/pages/manage.vue index 44be4cd8e..da85a2954 100644 --- a/resources/assets/js/pages/manage.vue +++ b/resources/assets/js/pages/manage.vue @@ -965,7 +965,9 @@ export default { }).then(({data}) => { this.exportLoadIng--; this.exportTaskShow = false; - $A.downFile(data.url); + this.$store.dispatch('downUrl', { + url: data.url + }); }).catch(({msg}) => { this.exportLoadIng--; $A.modalError(msg); diff --git a/resources/assets/js/pages/manage/components/DialogView.vue b/resources/assets/js/pages/manage/components/DialogView.vue index bb901cfe5..463f1cb4d 100644 --- a/resources/assets/js/pages/manage/components/DialogView.vue +++ b/resources/assets/js/pages/manage/components/DialogView.vue @@ -262,7 +262,7 @@ export default { content: `${this.msgData.msg.name} (${$A.bytesToSize(this.msgData.msg.size)})`, okText: '立即下载', onOk: () => { - $A.downFile($A.apiUrl(`dialog/msg/download?msg_id=${this.msgData.id}&token=${this.userToken}`)) + this.$store.dispatch('downUrl', $A.apiUrl(`dialog/msg/download?msg_id=${this.msgData.id}`)) } }); } diff --git a/resources/assets/js/pages/manage/components/TaskDetail.vue b/resources/assets/js/pages/manage/components/TaskDetail.vue index eafeb322f..0ef4ce922 100644 --- a/resources/assets/js/pages/manage/components/TaskDetail.vue +++ b/resources/assets/js/pages/manage/components/TaskDetail.vue @@ -1271,7 +1271,7 @@ export default { content: `${file.name} (${$A.bytesToSize(file.size)})`, okText: '立即下载', onOk: () => { - $A.downFile($A.apiUrl(`project/task/filedown?file_id=${file.id}&token=${this.userToken}`)) + this.$store.dispatch('downUrl', $A.apiUrl(`project/task/filedown?file_id=${file.id}`)) } }); } diff --git a/resources/assets/js/pages/manage/file.vue b/resources/assets/js/pages/manage/file.vue index 062388b76..5280adb9f 100644 --- a/resources/assets/js/pages/manage/file.vue +++ b/resources/assets/js/pages/manage/file.vue @@ -337,7 +337,7 @@ v-model="pasteShow" :title="$L(pasteTitle)" :cancel-text="$L('取消')" - :ok-text="$L('发送')" + :ok-text="$L('立即上传')" :enter-ok="true" @on-ok="pasteSend">