feat: 修复 Electron protocol 上传文件问题

This commit is contained in:
purocean
2021-06-27 20:45:39 +08:00
parent 1d2789d101
commit f4cdd9624d
4 changed files with 12 additions and 7 deletions
+1 -1
View File
@@ -285,7 +285,7 @@ if (!gotTheLock) {
{
type: 'radio',
checked: urlMode === 'scheme',
label: '正式端口(Schema)',
label: '正式端口(Scheme)',
click: () => {
urlMode = 'scheme'
reload()
+2 -2
View File
@@ -88,9 +88,9 @@ const checkHash = (repo: string, p: string, oldHash: string) => {
return oldHash === hash(repo, p)
}
const upload = (repo: string, file: any, path: string) => {
const upload = (repo: string, buffer: Buffer, path: string) => {
if (readonly) throw new Error('只读模式')
write(repo, path, fs.readFileSync(file.path))
write(repo, path, buffer)
}
const travels = (location: string, repo: string, basePath: string, markedFiles: MarkedFile[] | null = null): any => {
+7 -2
View File
@@ -63,7 +63,9 @@ const attachment = async (ctx: any, next: any) => {
if (ctx.method === 'POST') {
const path = ctx.request.body.path
const repo = ctx.request.body.repo
file.upload(repo, ctx.request.files.attachment, path)
const attachment = ctx.request.body.attachment
const buffer = Buffer.from(attachment.substring(attachment.indexOf(',') + 1), 'base64')
file.upload(repo, buffer, path)
ctx.body = result('ok', '上传成功', path)
} else if (ctx.method === 'GET') {
ctx.type = mime.getType(ctx.query.path)
@@ -251,7 +253,10 @@ const server = (port = 3000) => {
multipart: true,
formLimit: '20mb',
jsonLimit: '20mb',
textLimit: '20mb'
textLimit: '20mb',
formidable: {
maxFieldsSize: 268435456
}
}))
app.use(async (ctx: any, next: any) => await wrapper(ctx, next, fileContent))
+2 -2
View File
@@ -137,7 +137,7 @@ const search = async (repo: string, text: string) => {
const upload = async (repo: string, belongPath: string, uploadFile: any, name: string | null = null): Promise<{repo: string; path: string; relativePath: string}> => {
return new Promise((resolve, reject) => {
const fr = new FileReader()
fr.readAsBinaryString(uploadFile)
fr.readAsDataURL(uploadFile)
fr.onloadend = async () => {
try {
const filename = name || Crypto.binMd5(fr.result).substr(0, 8) + extname(uploadFile.name)
@@ -152,7 +152,7 @@ const upload = async (repo: string, belongPath: string, uploadFile: any, name: s
)
formData.append('repo', repo)
formData.append('path', path)
formData.append('attachment', uploadFile)
formData.append('attachment', fr.result as string)
await fetchHttp('/api/attachment', { method: 'POST', body: formData })