|
|
@@ -5,6 +5,7 @@ |
|
|
|
import { fetchApi, wrapErrorHint } from '@/utils/request'; |
|
|
|
import { notify, firstCharToUpperCase } from '@/utils/tool'; |
|
|
|
import { identity } from 'lodash'; |
|
|
|
import Vue from 'vue'; |
|
|
|
|
|
|
|
export const isClient = !!global.electron; // process.env.IS_CLIENT; |
|
|
|
|
|
|
@@ -110,71 +111,82 @@ const system = { |
|
|
|
const filePath = filePaths[0]; |
|
|
|
const extensionedFileName = filePath.split(/\/|\\/g).pop(); |
|
|
|
|
|
|
|
const tempFilePaths = extensionedFileName.split('.'); |
|
|
|
const extension = tempFilePaths.length > 1 ? tempFilePaths.pop() : ''; |
|
|
|
const fileName = tempFilePaths.join('.'); |
|
|
|
|
|
|
|
const maybeFile = fileList.find(iFile => `${iFile.archName}${iFile.extension ? `.${iFile.extension}` : ''}` === extensionedFileName); |
|
|
|
// 检测当前工作目录中是否存在同名文件 |
|
|
|
if(maybeFile) { |
|
|
|
let confirmRes = false; |
|
|
|
try { |
|
|
|
await Vue.prototype.$confirm('当前文件夹存在同名文件,是否上传并覆盖?'); |
|
|
|
confirmRes = true; |
|
|
|
} catch(e) { console.log('user canceled'); } |
|
|
|
if(!confirmRes) return; |
|
|
|
} |
|
|
|
|
|
|
|
const uploadFile = maybeFile |
|
|
|
? firstCharToUpperCase({ ...maybeFile, ModifyUserId: sessionStorage.userId }) |
|
|
|
: { |
|
|
|
// 文件名称 不带扩展名 |
|
|
|
ArchName: fileName, |
|
|
|
// CommonStatus: 0, |
|
|
|
// CreateTime: "string", |
|
|
|
// 文件上传者Id |
|
|
|
CreateUserId: sessionStorage.userId, |
|
|
|
// Deleted: 0, |
|
|
|
Extension: extension, |
|
|
|
// 文件大小 单位? |
|
|
|
// FileSize: +size, |
|
|
|
// 所处文件夹id |
|
|
|
FolderId: folderId, |
|
|
|
// 所处文件夹层级,拼接符:_ |
|
|
|
FolderLevelId: levelId, |
|
|
|
// Id: 0, |
|
|
|
// IpfsCid: hash, |
|
|
|
// IsShowRecycle: 0, |
|
|
|
// Milestone: 0, |
|
|
|
// ModifyTime: "string", |
|
|
|
// ModifyUserId: 0, |
|
|
|
// 项目id |
|
|
|
ProjId: projectId, |
|
|
|
// ShowUrl: "string", |
|
|
|
// Status: 0, |
|
|
|
// Version: 0, |
|
|
|
// WorkStatus: 0 |
|
|
|
}; |
|
|
|
|
|
|
|
const socket = io('upload'); |
|
|
|
socket.on('open', () => { |
|
|
|
const data = [filePath, extensionedFileName, projectName, folderName].join('|'); |
|
|
|
socket.send(data); |
|
|
|
onProgressHandler({ process: 0 }, uploadFile); |
|
|
|
}); |
|
|
|
|
|
|
|
socket.on('message', async (e) => { |
|
|
|
console.log('receive download file message:', e); |
|
|
|
try { |
|
|
|
const { size, process, hash } = JSON.parse(e.data); |
|
|
|
onProgressHandler(e); |
|
|
|
const progressData = JSON.parse(e.data); |
|
|
|
const { size, process, hash } = progressData; |
|
|
|
onProgressHandler(progressData, uploadFile); |
|
|
|
if(process !== 100 || !hash) return; |
|
|
|
socket.close(); |
|
|
|
// {"size":"88.69","currentSize":"88.69","unit":"KiB","process":100,"hash":""} |
|
|
|
// {"size":"","currentSize":"","unit":"","process":100,"hash":"QmPJ9i4z5UdoQpLH1DrkhZiTZra2rGicXiPabiLw4LvTmX"} |
|
|
|
const maybeFile = fileList.find(iFile => `${iFile.archName}${iFile.extension ? `.${iFile.extension}` : ''}` === extensionedFileName); |
|
|
|
if(maybeFile) { // 更新同名文件 |
|
|
|
const copyFile = firstCharToUpperCase({ ...maybeFile, ipfsCid: hash, ModifyUserId: sessionStorage.userId }); |
|
|
|
|
|
|
|
const res = await fetchApi('file/updateFile', copyFile); |
|
|
|
wrapErrorHint(res); |
|
|
|
if(res.Code === 0) { notify.success('上传成功, 已覆盖同名文件'); } |
|
|
|
return; |
|
|
|
} |
|
|
|
const tempFilePaths = extensionedFileName.split('.'); |
|
|
|
const extension = tempFilePaths.length > 1 ? tempFilePaths.pop() : ''; |
|
|
|
const fileName = tempFilePaths.join('.'); |
|
|
|
// 1.get file ipfscid |
|
|
|
const fileObj = { |
|
|
|
// 文件名称 不带扩展名 |
|
|
|
ArchName: fileName, |
|
|
|
// CommonStatus: 0, |
|
|
|
// CreateTime: "string", |
|
|
|
// 文件上传者Id |
|
|
|
CreateUserId: sessionStorage.userId, |
|
|
|
// Deleted: 0, |
|
|
|
Extension: extension, |
|
|
|
// 文件大小 单位? |
|
|
|
FileSize: +size, |
|
|
|
// 所处文件夹id |
|
|
|
FolderId: folderId, |
|
|
|
// 所处文件夹层级,拼接符:_ |
|
|
|
FolderLevelId: levelId, |
|
|
|
// Id: 0, |
|
|
|
IpfsCid: hash, |
|
|
|
// IsShowRecycle: 0, |
|
|
|
// Milestone: 0, |
|
|
|
// ModifyTime: "string", |
|
|
|
// ModifyUserId: 0, |
|
|
|
// 项目id |
|
|
|
ProjId: projectId, |
|
|
|
// ShowUrl: "string", |
|
|
|
// Status: 0, |
|
|
|
// Version: 0, |
|
|
|
// WorkStatus: 0 |
|
|
|
} |
|
|
|
// console.log('trying to execute file/addFile:', fileObj); |
|
|
|
// socket.close(); |
|
|
|
const res = await fetchApi('file/addFile', fileObj); |
|
|
|
// const maybeFile = fileList.find(iFile => `${iFile.archName}${iFile.extension ? `.${iFile.extension}` : ''}` === extensionedFileName); |
|
|
|
uploadFile.FileSize = +size; |
|
|
|
uploadFile.IpfsCid = hash; |
|
|
|
const res = await fetchApi(`file/${maybeFile ? 'updateFile' : 'addFile'}`, uploadFile); |
|
|
|
wrapErrorHint(res); |
|
|
|
if(res.Code !== 0) return; |
|
|
|
notify.success('上传成功'); |
|
|
|
onSuccessHandler(fileObj); |
|
|
|
notify.success(maybeFile ? '上传成功, 已覆盖同名文件' : '上传成功'); |
|
|
|
onSuccessHandler(uploadFile); |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
console.error('socket-upload-file parse data have error:', e); |
|
|
|
// todo 上传失败 |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
}), |
|
|
@@ -206,6 +218,7 @@ const system = { |
|
|
|
return; |
|
|
|
} catch(err) { |
|
|
|
console.error('socket-update-file parse data have error:', e); |
|
|
|
// todo 上传失败 |
|
|
|
} |
|
|
|
}); |
|
|
|
}), |
|
|
|