Skip to content

Commit 2b863b4

Browse files
committed
feat: 🎸 支持使用 upic 上传图床
1 parent 5c8713f commit 2b863b4

File tree

7 files changed

+71
-13
lines changed

7 files changed

+71
-13
lines changed

src-tauri/capabilities/migrated.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@
2222
"allow": [{ "path": "**" }]
2323
},
2424
"shell:default",
25+
{
26+
"identifier": "shell:allow-execute",
27+
"allow": [
28+
{
29+
"name": "exec-sh",
30+
"cmd": "sh",
31+
"args": [
32+
"-c",
33+
{
34+
"validator": "\\S+"
35+
}
36+
],
37+
"sidecar": false
38+
}
39+
]
40+
},
2541
"dialog:allow-save",
2642
"dialog:allow-message",
2743
"dialog:allow-open",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"migrated":{"identifier":"migrated","description":"permissions that were migrated from v1","local":true,"windows":["*"],"permissions":["path:default","event:default","window:default","window:allow-start-dragging","webview:allow-print","app:default","image:default","resources:default","menu:default","tray:default","shell:allow-open","fs:default","fs:write-all",{"identifier":"fs:scope","allow":[{"path":"**"}]},"shell:default","dialog:allow-save","dialog:allow-message","dialog:allow-open","dialog:allow-confirm","os:allow-platform","os:allow-version","os:allow-os-type","os:allow-family","os:allow-arch","os:allow-exe-extension","os:allow-locale","os:allow-hostname","window:allow-start-dragging","window:allow-set-fullscreen","window:allow-minimize","window:allow-close","window:allow-maximize","updater:default","process:default","clipboard-manager:default","clipboard-manager:allow-write-text"]}}
1+
{"migrated":{"identifier":"migrated","description":"permissions that were migrated from v1","local":true,"windows":["*"],"permissions":["path:default","event:default","window:default","window:allow-start-dragging","webview:allow-print","app:default","image:default","resources:default","menu:default","tray:default","shell:allow-open","fs:default","fs:write-all",{"identifier":"fs:scope","allow":[{"path":"**"}]},"shell:default",{"identifier":"shell:allow-execute","allow":[{"args":["-c",{"validator":"\\S+"}],"cmd":"sh","name":"exec-sh","sidecar":false}]},"dialog:allow-save","dialog:allow-message","dialog:allow-open","dialog:allow-confirm","os:allow-platform","os:allow-version","os:allow-os-type","os:allow-family","os:allow-arch","os:allow-exe-extension","os:allow-locale","os:allow-hostname","window:allow-start-dragging","window:allow-set-fullscreen","window:allow-minimize","window:allow-close","window:allow-maximize","updater:default","process:default","clipboard-manager:default","clipboard-manager:allow-write-text"]}}

src/components/SettingModal.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ const data = [
2828
const uploadOptions = [
2929
{ value: 'none', name: 'None' },
3030
{ value: 'PicGo', name: 'PicGo' },
31+
{ value: 'uPic', name: 'uPic(only for mac)' },
3132
{ value: 'custom', name: 'Custom' },
32-
{ value: 'uPic', name: 'uPic(develop)' },
33-
{ value: 'Picsee', name: 'Picsee(develop)' },
3433
]
3534

3635
export default function SettingModal() {
@@ -118,6 +117,9 @@ export default function SettingModal() {
118117
<Input defaultValue={config?.command} name="command" />
119118
</div>
120119
</div>
120+
<div className="pl-28 text-xs mt-1 text-gray-500">
121+
{t('图床设置选择 custom,会将本地图片路径传递在自定义命令后')}
122+
</div>
121123

122124
<div className="mt-4">
123125
<div className="text-sm text-gray-500 dark:text-white flex items-center">

src/lib/tauri.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ export const uploadImage = async (blob) => {
155155
fileName = `/img/${time}.png`
156156
}
157157
await writeFile(`${dirPath}${fileName}`, contents)
158-
return `./img/${time}.png`
158+
return {
159+
path: `./img/${time}.png`,
160+
fullPath: `${dirPath}${fileName}`,
161+
}
159162
}
160163

161164
export const downloadFile = async (fileName: string, content: string) => {

src/monaco/pasteImage.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
2-
import { uploadImage } from '@/lib/bindings'
2+
import { uploadImage, remove } from '@/lib/bindings'
33
import { getItem } from '@/utils/storage'
4+
import { Command } from '@tauri-apps/plugin-shell'
45

56
const codeToUpload = {
6-
none: uploadImage,
7+
none: async (blob) => {
8+
return uploadImage(blob).path
9+
},
710
PicGo: async () => {
811
try {
912
const res = await fetch('http://127.0.0.1:36677/upload', {
@@ -14,11 +17,40 @@ const codeToUpload = {
1417
return '上传失败'
1518
}
1619
},
17-
uPic: async () => {
18-
return '上传失败'
20+
uPic: async (blob) => {
21+
// 上传图片
22+
const res = await uploadImage(blob)
23+
try {
24+
let result = await Command.create('exec-sh', [
25+
'-c',
26+
`/Applications/uPic.app/Contents/MacOS/uPic -o url -u ${escape(
27+
res.fullPath
28+
)}`,
29+
]).execute()
30+
31+
// 上传成功后删除图片
32+
await remove(res.fullPath)
33+
return result?.stdout.split('Output URL:')[1]?.trim()
34+
} catch (error) {
35+
return '上传失败'
36+
}
1937
},
20-
custom: async () => {
21-
return '上传失败'
38+
custom: async (blob, command) => {
39+
// 上传图片
40+
const res = await uploadImage(blob)
41+
try {
42+
let result = await Command.create('exec-sh', [
43+
'-c',
44+
`${command} ${escape(res.fullPath)}`,
45+
]).execute()
46+
// 上传成功后删除图片
47+
await remove(res.fullPath)
48+
49+
return result?.stdout?.trim() || '执行失败'
50+
} catch (error) {
51+
console.log('error', error)
52+
return '上传失败'
53+
}
2254
},
2355
}
2456

@@ -38,7 +70,10 @@ export function listenPaste(editor) {
3870
upload: 'none',
3971
command: '',
4072
}
41-
const fileName = await codeToUpload[config.upload](blob)
73+
const fileName = await codeToUpload[config.upload](
74+
blob,
75+
config.command
76+
)
4277

4378
editor.executeEdits('', [
4479
{

src/utils/locales/en/translation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@
6666
"Downloading": "Downloading",
6767
"Continue": "Continue",
6868
"Cancel": "Cancel",
69-
"Support HTML": "Support HTML"
69+
"Support HTML": "Support HTML",
70+
"图床设置选择 custom,会将本地图片路径传递在自定义命令后": "Upload Picture select custom, will pass the local image path after the custom command"
7071
}

src/utils/locales/zh-CN/translation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@
6666
"Downloading": "下载中",
6767
"Continue": "确认",
6868
"Cancel": "取消",
69-
"Support HTML": "支持HTML"
69+
"Support HTML": "支持HTML",
70+
"图床设置选择 custom,会将本地图片路径传递在自定义命令后": "图床设置选择 custom,会将本地图片路径传递在自定义命令后"
7071
}

0 commit comments

Comments
 (0)