-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
369 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import * as vscode from 'vscode'; | ||
import { | ||
UploadType, | ||
CustomUploader, | ||
BackfillImageProps, | ||
BackfillImage, | ||
} from '../types'; | ||
import axios, { AxiosResponse } from 'axios'; | ||
|
||
export interface FileInfo { | ||
name: string; | ||
type: string; | ||
path: string; | ||
size: number; | ||
} | ||
|
||
export interface UploadFileHandlerRes extends BackfillImage { | ||
name: string; | ||
url: string; | ||
poster?: string; | ||
} | ||
|
||
export const uploadFileHandler = async (fileInfo: FileInfo) => { | ||
const { name = '', type = '', path = '' } = fileInfo; | ||
|
||
const UploadType = vscode.workspace | ||
.getConfiguration('cherryMarkdown') | ||
.get<UploadType>('UploadType'); | ||
|
||
const res: UploadFileHandlerRes = { name, url: '' }; | ||
|
||
const BackfillImageProps = vscode.workspace | ||
.getConfiguration('cherryMarkdown') | ||
.get<BackfillImageProps>('BackfillImageProps', []); | ||
|
||
BackfillImageProps.reduce((prev, curr) => ((prev[curr] = true), prev), res); | ||
|
||
switch (UploadType) { | ||
case 'CustomUploader': | ||
// const CustomUploader = vscode.workspace | ||
// .getConfiguration('cherryMarkdown') | ||
// .get<CustomUploader>('CustomUploader'); | ||
|
||
// if (CustomUploader?.enable !== true) { | ||
// vscode.window.showInformationMessage('请完善自定义上传配置'); | ||
// throw new Error('请完善自定义上传配置'); | ||
// } | ||
// if (/^(http|https):\/\//.test(CustomUploader.url) == false) { | ||
// vscode.window.showInformationMessage('自定义上传地址格式不正确'); | ||
// throw new Error('自定义上传地址格式不正确'); | ||
// } | ||
// const file = await vscode.workspace.fs.readFile(vscode.Uri.file(path)); | ||
// // 将file上传到自定义的地址 | ||
// // 这里涉及到一些上传服务需要签名校验,并且响应体格式不一致,这里要再讨论 | ||
// const customUpload = await axios.post(CustomUploader.url, file); | ||
vscode.window.showInformationMessage('自定义上传暂未开发'); | ||
throw new Error('自定义上传暂未开发'); | ||
break; | ||
case 'PicGoServer': | ||
const PicGoServer = vscode.workspace | ||
.getConfiguration('cherryMarkdown') | ||
.get<string>('PicGoServer', 'http://127.0.0.1:36677/upload'); | ||
// 请求PicGo服务 | ||
const upload = await axios.post< | ||
any, | ||
AxiosResponse<{ success: boolean; result: string[] }>, | ||
{ list: string[] } | ||
>( | ||
PicGoServer, | ||
{ | ||
list: [path], | ||
}, | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
); | ||
if (upload.data?.success !== true) { | ||
throw new Error('上传失败'); | ||
} else { | ||
res.url = upload.data?.result?.[0] ?? ''; | ||
} | ||
break; | ||
default: | ||
if (type.startsWith('image')) { | ||
// 读取图片转为base64 | ||
const file = await vscode.workspace.fs.readFile(vscode.Uri.file(path)); | ||
const base64 = Buffer.from(file).toString('base64'); | ||
res.url = `data:${type};base64,${base64}`; | ||
} else { | ||
vscode.window.showInformationMessage('未指定上传服务时暂时只支持图片'); | ||
throw new Error('未指定上传服务时暂时只支持图片'); | ||
} | ||
break; | ||
} | ||
return res; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './upload'; |
Oops, something went wrong.