From 267bee6dd4f019871285ec0d4ebe2c92c950ba9c Mon Sep 17 00:00:00 2001 From: zhuhao <979263092@qq.com> Date: Wed, 10 Jul 2024 20:26:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD=EF=BC=8C?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=8D=E5=88=B6=EF=BC=8C=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=9C=AC=E5=9C=B0=E9=BB=98=E8=AE=A4=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E6=89=93=E5=BC=80=E7=9A=84API=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 + src/main/file.js | 97 +++++++++++++++++++ src/main/index.js | 7 +- .../src/components/file-upload/index.vue | 20 +++- src/renderer/src/views/prepare/index.vue | 2 +- 5 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 src/main/file.js diff --git a/package.json b/package.json index 8e45d8a..dfd9830 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,8 @@ "dependencies": { "@electron-toolkit/preload": "^3.0.1", "@electron-toolkit/utils": "^3.0.0", + "crypto-js": "^4.2.0", + "electron-dl-manager": "^3.0.0", "electron-updater": "^6.1.7", "element-plus": "^2.7.6", "js-cookie": "^3.0.5", diff --git a/src/main/file.js b/src/main/file.js new file mode 100644 index 0000000..1551855 --- /dev/null +++ b/src/main/file.js @@ -0,0 +1,97 @@ +const fs = require('fs') +const path = require('path') +import { ElectronDownloadManager } from 'electron-dl-manager'; +const manager = new ElectronDownloadManager(); +export default async function ({ app, shell, BrowserWindow, ipcMain }) { + + const userDataPath = app.getPath('userData') + //默认浏览器打开url + ipcMain.on('open-url-browser', (e, url) => { + shell.openPath(url) + }) + //使用默认应用打开本地文件 + ipcMain.on('open-path-app', (e, path) => { + shell.openExternal(path) + }) + + //复制文件 + ipcMain.on('copy-file-default', (e, { source, destination }) => { + copyFile(source, destination, (error, filePath) => { + e.reply('copy-file-default-reply', { error, filePath }) + }) + }) + + //下载文件 + ipcMain.on('download-file-default', (e,url) => { + createFolder('selfFile').then(async ()=>{ + const browserWindow = BrowserWindow.fromId(e.sender.id) + const id = await manager.download({ + window: browserWindow, + url: url, + directory: userDataPath + '/selfFile/', + callbacks: { + onDownloadStarted: async ({ id, item, webContents }) => { + // Do something with the download id + }, + onDownloadProgress: async ({ id, item, percentCompleted }) => { + browserWindow.webContents.invoke('download-progress', { + id, + percentCompleted, + // Get the number of bytes received so far + bytesReceived: item.getReceivedBytes(), + }); + }, + onDownloadCompleted: async ({ id, item }) => { + console.log(item) + }, + onDownloadCancelled: async () => {}, + onDownloadInterrupted: async () => {}, + onError: (err, data) => {}, + } + }); + }) + }) + + function copyFile(source, destination, callback) { + let path = userDataPath + '\\selfFile\\' + destination + createFolder('selfFile').then(() => { + const readStream = fs.createReadStream(source) + const writeStream = fs.createWriteStream(path) + + readStream.on('error', (error) => { + callback(error, null) + }) + writeStream.on('error', (error) => { + callback(error, null) + }) + writeStream.on('close', () => { + callback(null, path) + }) + + readStream.pipe(writeStream) + }) + } + + function createFolder(folderName) { + return new Promise((resolve, reject) => { + const folderPath = path.join(userDataPath, folderName) + // 异步检查文件夹是否存在,不存在则创建 + fs.access(folderPath, fs.constants.F_OK, (err) => { + if (err) { + fs.mkdir(folderPath, { recursive: true }, (mkdirErr) => { + if (mkdirErr) { + console.error(mkdirErr) + reject() + } else { + console.log(`Folder ${folderName} created successfully.`) + resolve() + } + }) + } else { + console.log(`Folder ${folderName} already exists.`) + resolve() + } + }) + }) + } +} diff --git a/src/main/index.js b/src/main/index.js index 7f853f5..72d5a72 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -2,7 +2,9 @@ import { app, shell, BrowserWindow, ipcMain } from 'electron' import { join } from 'path' import { electronApp, optimizer, is } from '@electron-toolkit/utils' import icon from '../../resources/icon.png?asset' +import File from './file' +File({ app, shell, BrowserWindow, ipcMain }) function createWindow() { // Create the browser window. const mainWindow = new BrowserWindow({ @@ -35,7 +37,7 @@ function createWindow() { if (is.dev && process.env['ELECTRON_RENDERER_URL']) { mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL']) // mainWindow.loadURL('https://file.ysaix.com:7868/') - + } else { // mainWindow.loadURL('https://file.ysaix.com:7868/') mainWindow.loadFile(join(__dirname, '../renderer/index.html')) @@ -102,8 +104,7 @@ ipcMain.on('close-window', () => { const win = BrowserWindow.getFocusedWindow(); win.close(); }); -console.log(100) ipcMain.on('set-winsize', (e, {x, y})=>{ const win = BrowserWindow.getFocusedWindow(); win.setSize(x,y); -}) \ No newline at end of file +}) diff --git a/src/renderer/src/components/file-upload/index.vue b/src/renderer/src/components/file-upload/index.vue index e328e0f..5195a8c 100644 --- a/src/renderer/src/components/file-upload/index.vue +++ b/src/renderer/src/components/file-upload/index.vue @@ -4,8 +4,9 @@ :action="uploadUrl" name="file" :headers="headers" - :data="uploadData" + :data="uploadDatas" :show-file-list="false" + :before-upload="beforeUpload" class="editor-img-uploader" > @@ -15,6 +16,7 @@ diff --git a/src/renderer/src/views/prepare/index.vue b/src/renderer/src/views/prepare/index.vue index c485a39..7638d38 100644 --- a/src/renderer/src/views/prepare/index.vue +++ b/src/renderer/src/views/prepare/index.vue @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file