Last active
September 17, 2023 01:11
-
-
Save DipsySu/7135d14a6642bf760633a6b64d0711e9 to your computer and use it in GitHub Desktop.
热键触发图片剪切板
This file contains hidden or 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
| package main | |
| import ( | |
| "context" | |
| "fmt" | |
| "github.com/go-vgo/robotgo" | |
| "github.com/qiniu/go-sdk/v7/auth/qbox" | |
| "github.com/qiniu/go-sdk/v7/sms/bytes" | |
| "github.com/qiniu/go-sdk/v7/storage" | |
| hook "github.com/robotn/gohook" | |
| "golang.design/x/clipboard" | |
| "strconv" | |
| "strings" | |
| "time" | |
| ) | |
| type Config struct { | |
| AccessKey string | |
| SecretKey string | |
| HostURL string | |
| Bucket string | |
| } | |
| // 为了模拟,我们直接定义一个config。实际情况中,你可以从 config.json 文件中读取它。 | |
| var config = Config{ | |
| AccessKey: "", | |
| SecretKey: "", | |
| HostURL: "", | |
| Bucket: "", | |
| } | |
| var gifAddr = "/Users/su/Movies/Kaptures/" | |
| func main() { | |
| hotKeyClick() | |
| } | |
| func readClipboardContent(format clipboard.Format) string { | |
| data := clipboard.Read(format) | |
| return string(data) | |
| } | |
| func hotKeyClick() { | |
| robotgo.EventHook(hook.KeyDown, []string{"u", "command"}, func(event hook.Event) { | |
| data := readClipboardContent(clipboard.FmtImage) | |
| uploadContent([]byte(data), "png") | |
| }) | |
| robotgo.EventHook(hook.KeyDown, []string{"t", "command"}, func(event hook.Event) { | |
| gifName := readClipboardContent(clipboard.FmtText) | |
| if strings.HasSuffix(gifName, ".gif") { | |
| localFile := gifAddr + gifName | |
| uploadContent([]byte(localFile), "gif") | |
| } | |
| }) | |
| s := robotgo.EventStart() | |
| <-robotgo.EventProcess(s) | |
| } | |
| func uploadContent(content []byte, fileType string) { | |
| var ( | |
| accessKey = config.AccessKey | |
| secretKey = config.SecretKey | |
| bucket = config.Bucket | |
| hostname = config.HostURL | |
| uploadFileName = strconv.FormatInt(time.Now().Unix(), 10) + "." + fileType | |
| ) | |
| putPolicy := storage.PutPolicy{ | |
| Scope: bucket, | |
| } | |
| mac := qbox.NewMac(accessKey, secretKey) | |
| uploadToken := putPolicy.UploadToken(mac) | |
| cfg := storage.Config{ | |
| Zone: &storage.ZoneHuadong, | |
| UseHTTPS: true, | |
| UseCdnDomains: false, | |
| } | |
| formUploader := storage.NewFormUploader(&cfg) | |
| ret := storage.PutRet{} | |
| var err error | |
| if fileType == "gif" { | |
| err = formUploader.PutFile(context.Background(), &ret, uploadToken, uploadFileName, string(content), &storage.PutExtra{}) | |
| } else { | |
| dataLen := int64(len(content)) | |
| err = formUploader.Put(context.Background(), &ret, uploadToken, uploadFileName, bytes.NewReader(content), dataLen, &storage.PutExtra{}) | |
| } | |
| if err != nil { | |
| fmt.Println(err) | |
| return | |
| } | |
| fmt.Println(ret.Key, ret.Hash) | |
| fileURL := hostname + ret.Key | |
| markdownStr := "" | |
| clipboard.Write(clipboard.FmtText, []byte(markdownStr)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment