整理下载文件跳转

This commit is contained in:
kuaifan 2022-03-31 12:56:30 +08:00
parent cfd1fc275b
commit 457efa1c79
9 changed files with 88 additions and 112 deletions

View File

@ -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 "<script>setTimeout(function() { window.location.href = '{$url}'; }, 0)</script>";
}
//
$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

88
electron/electron.js vendored
View File

@ -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}

View File

@ -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("?&", "?"), '?');
},
/**

View File

@ -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

View File

@ -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);

View File

@ -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}`))
}
});
}

View File

@ -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}`))
}
});
}

View File

@ -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">
<div class="dialog-wrapper-paste">
@ -1030,7 +1030,7 @@ export default {
content: `${item.name}.${item.ext} (${$A.bytesToSize(item.size)})`,
okText: '立即下载',
onOk: () => {
$A.downFile($A.apiUrl(`file/content?id=${item.id}&down=yes&token=${this.userToken}`))
this.$store.dispatch('downUrl', $A.apiUrl(`file/content?id=${item.id}&down=yes`))
}
});
break;

View File

@ -139,6 +139,36 @@ export default {
})
},
/**
* 下载文件
* @param state
* @param data
*/
downUrl({state}, data) {
if (!data) {
return
}
let url = data;
let params = {
token: state.userToken
};
if ($A.isJson(data)) {
url = data.url;
params = data.params || {};
}
url = $A.urlAddParams(url, params);
url = $A.apiUrl(`../download?key=${encodeURIComponent(url)}`)
if ($A.Electron) {
$A.Electron.request({action: 'openExternal', url}, () => {
// 成功
}, () => {
// 失败
});
} else {
window.open(url)
}
},
/**
* 切换面板变量
* @param state