-
Notifications
You must be signed in to change notification settings - Fork 0
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
gitlayzer
committed
Mar 21, 2024
1 parent
db68e1b
commit d1fe345
Showing
6 changed files
with
161 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,23 @@ | ||
# picturectl | ||
Typora Upload Image Plugin | ||
# Picturectl Typora Upload Image Plugin | ||
|
||
## 0. Overview | ||
这个小工具是根据 [Telegraph-Image](https://github.com/cf-pages/Telegraph-Image) 这个图床项目实现的 Typora 的插件 | ||
|
||
## 1. Introduction | ||
确保自己本地有 Go 语言的开发环境 | ||
- 代码克隆 git clone https://github.com/gitlayzer/picturectl.git | ||
- 编译 windows 平台:make build-windows | ||
- 编译 linux 平台:make build-linux | ||
- 生成的文件在 bin 目录下,可以根据自己的需求放到合适的位置使用 | ||
|
||
## 2. Usage | ||
这里以 windows 作为演示 | ||
- make build-windows | ||
- 将生成的 picturectl.exe 放到你的 Typora 图片上传插件目录下 | ||
- 打开 Typora 设置,找到偏好设置 | ||
- 找到图像,在插入图片时选择上传图片 | ||
- 在上传服务设定的地方选择自定义命令 | ||
- 在命令内写入自己编译生成的 picturectl.exe 的路径 | ||
- 需要注意的是,这里需要在命令后面跟上你的图片的服务器地址 | ||
- 比如:picturectl.exe F:\Software\Typora\picturectl.exe https://picture.xxxxxxxxxxx.com.cn | ||
![image-20240321154930617](https://picture.devops-engineer.com.cn/file/0609089c525fe6b0dad1e.png) |
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,25 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gitlayzer/picturectl/pkg" | ||
"log" | ||
"os" | ||
) | ||
|
||
func Run() { | ||
// 判断第一个参数是否为一个URL | ||
if len(os.Args) < 3 { | ||
fmt.Println("Usage: picturectl <pictureUrl> <imagePath>") | ||
return | ||
} | ||
|
||
pictureUrl := os.Args[1] | ||
|
||
imagePath := os.Args[2] | ||
|
||
err := pkg.UploadImage(pictureUrl, imagePath, "file") | ||
if err != nil { | ||
log.Fatalf("Failed to upload image: %v", err) | ||
} | ||
} |
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,3 @@ | ||
module github.com/gitlayzer/picturectl | ||
|
||
go 1.22.0 |
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,7 @@ | ||
package main | ||
|
||
import "github.com/gitlayzer/picturectl/cmd" | ||
|
||
func main() { | ||
cmd.Run() | ||
} |
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,15 @@ | ||
# 编译 windows 环境下的 Go 程序 | ||
build-windows: | ||
go build -o bin/picturectl.exe main.go | ||
|
||
# 编译 linux 环境下的 Go 程序 | ||
build-linux: | ||
go build -o bin/picturectl main.go | ||
|
||
# 编译 macOS 环境下的 Go 程序 | ||
build-macos: | ||
go build -o bin/picturectl main.go | ||
|
||
# 运行本地的 Go 程序 | ||
run: | ||
go run main.go |
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,88 @@ | ||
package pkg | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"mime/multipart" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type Image struct { | ||
Src string `json:"src"` | ||
} | ||
|
||
func UploadImage(cfPictureURL, filePath, fileFieldName string) error { | ||
// 拼接上传图片的URL | ||
postUrl := cfPictureURL + "/upload" | ||
// 读取文件 | ||
file, err := os.Open(filePath) | ||
if err != nil { | ||
return fmt.Errorf("unable to open file: %v", err) | ||
} | ||
defer file.Close() | ||
|
||
// 创建一个缓冲区来存储 multipart 的表单数据 | ||
var requestBody bytes.Buffer | ||
multipartWriter := multipart.NewWriter(&requestBody) | ||
|
||
// 创建表单文件部分 | ||
formFile, err := multipartWriter.CreateFormFile(fileFieldName, filepath.Base(file.Name())) | ||
if err != nil { | ||
return fmt.Errorf("partial failure to create form file: %v", err) | ||
} | ||
|
||
// 将文件数据复制到表单 | ||
_, err = io.Copy(formFile, file) | ||
if err != nil { | ||
return fmt.Errorf("failed to copy file to form: %v", err) | ||
} | ||
|
||
// 关闭multipart writer来设置表单的最终边界 | ||
multipartWriter.Close() | ||
|
||
// 创建HTTP请求 | ||
request, err := http.NewRequest(http.MethodPost, postUrl, &requestBody) | ||
if err != nil { | ||
return fmt.Errorf("failed to create HTTP request: %v", err) | ||
} | ||
// 设置Content-Type头部,这样服务器知道是multipart/form-data | ||
request.Header.Set("Content-Type", multipartWriter.FormDataContentType()) | ||
|
||
// 发送请求 | ||
client := &http.Client{} | ||
response, err := client.Do(request) | ||
if err != nil { | ||
return fmt.Errorf("sending request failed: %v", err) | ||
} | ||
defer response.Body.Close() | ||
|
||
// 检查响应状态码 | ||
if response.StatusCode != http.StatusOK { | ||
return fmt.Errorf("unexpected status code: %v", response.Status) | ||
} | ||
|
||
// 打印响应内容 | ||
responseBody := &bytes.Buffer{} | ||
_, err = responseBody.ReadFrom(response.Body) | ||
if err != nil { | ||
return fmt.Errorf("failed to read response: %v", err) | ||
} | ||
result := responseBody.String() | ||
|
||
// 解析JSON | ||
var images []Image | ||
err = json.Unmarshal([]byte(result), &images) | ||
if err != nil { | ||
log.Fatalf("JSON parsing error: %v", err) | ||
} | ||
|
||
// 检查是否有结果并打印出来 | ||
fmt.Println(cfPictureURL + images[0].Src) | ||
|
||
return nil | ||
} |