Skip to content

Commit

Permalink
refactor: re-design api to support multiple files copy & paste
Browse files Browse the repository at this point in the history
  • Loading branch information
YanxinTang committed Oct 30, 2020
1 parent b4a7adc commit 9170de4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ clipboard-online is an application to share cilpboard text between Windows and i

1. Run `clipboard-online` on your windows
2. Setup shortcuts on you iPhone/iPad (Open link from safari)
- Copy: [https://www.icloud.com/shortcuts/242c55e0895e4235875bc71f1f010199](https://www.icloud.com/shortcuts/242c55e0895e4235875bc71f1f010199)
- Paste: [https://www.icloud.com/shortcuts/6a46febf2f0c4ef4b00bbc41f03ccd2f](https://www.icloud.com/shortcuts/6a46febf2f0c4ef4b00bbc41f03ccd2f)
- Copy: [https://www.icloud.com/shortcuts/7f11d4ada58c452b9a2f59fb54d5a1f5](https://www.icloud.com/shortcuts/7f11d4ada58c452b9a2f59fb54d5a1f5)
- Paste: [https://www.icloud.com/shortcuts/701c8a6f64d846c9a69a28087c1dd034](https://www.icloud.com/shortcuts/701c8a6f64d846c9a69a28087c1dd034)
3. Have fun...😊

## Configuration
Expand Down
4 changes: 2 additions & 2 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ clipboard-online 是一款可以帮你在 💻Windows 和 📱iOS 之间分享

1. 在 Windows 上运行 `clipboard-online`
2. iPhone 或 iPad 上安装快捷指令 (在 safari 中打开链接)
- 复制: [https://www.icloud.com/shortcuts/242c55e0895e4235875bc71f1f010199](https://www.icloud.com/shortcuts/242c55e0895e4235875bc71f1f010199)
- 粘贴: [https://www.icloud.com/shortcuts/6a46febf2f0c4ef4b00bbc41f03ccd2f](https://www.icloud.com/shortcuts/6a46febf2f0c4ef4b00bbc41f03ccd2f)
- 复制: [https://www.icloud.com/shortcuts/7f11d4ada58c452b9a2f59fb54d5a1f5](https://www.icloud.com/shortcuts/7f11d4ada58c452b9a2f59fb54d5a1f5)
- 粘贴: [https://www.icloud.com/shortcuts/701c8a6f64d846c9a69a28087c1dd034](https://www.icloud.com/shortcuts/701c8a6f64d846c9a69a28087c1dd034)
3. 玩的开心...😊

## 配置
Expand Down
65 changes: 31 additions & 34 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func readBase64FromFile(path string) (string, error) {

// TextBody is a struct of request body when iOS send files to windows
type TextBody struct {
Text string `json:"text"`
Text string `json:"data"`
}

func setHandler(c *gin.Context) {
Expand All @@ -193,7 +193,7 @@ func setHandler(c *gin.Context) {
func setTextHandler(c *gin.Context, logger *logrus.Entry) {
var body TextBody
if err := c.ShouldBindJSON(&body); err != nil {
logger.WithError(err).Warn("failed to bind file body")
logger.WithError(err).Warn("failed to bind text body")
c.Status(http.StatusBadRequest)
return
}
Expand All @@ -215,31 +215,27 @@ func setTextHandler(c *gin.Context, logger *logrus.Entry) {

// FileBody is a struct of request body when iOS send files to windows
type FileBody struct {
NamesString string `json:"names"` // Name1\nName2...
EncodedFilesString string `json:"files"` // File1Base64\nFile2Base64...
Files []File `json:"data"`
}

// ByteFile is a struct to save uploaded file in memory
type ByteFile struct {
Name string // filename
Bytes []byte // bytes of file content
// File is a struct represtents request file
type File struct {
Name string `json:"name"` // filename
Base64 string `json:"base64"`
_bytes []byte `json:"-"` // don't use this directly. use *File.Bytes() to get bytes
}

// ByteFiles returns ByteFile list from request body
func (fb *FileBody) ByteFiles() ([]ByteFile, error) {
names := strings.Split(fb.NamesString, "\n")
encodedFiles := strings.Split(fb.EncodedFilesString, "\n")

byteFiles := make([]ByteFile, 0, len(encodedFiles))
for i, encodedFile := range encodedFiles {
fileBytes, err := base64.StdEncoding.DecodeString(encodedFile)
if err != nil {
// todo: warning log to file
continue
}
byteFiles = append(byteFiles, ByteFile{names[i], fileBytes})
// Bytes returns byte slice of file
func (f *File) Bytes() ([]byte, error) {
if len(f._bytes) > 0 {
return f._bytes, nil
}
fileBytes, err := base64.StdEncoding.DecodeString(f.Base64)
if err != nil {
return []byte{}, nil
}
return byteFiles, nil
f._bytes = fileBytes
return fileBytes, nil
}

func setFileHandler(c *gin.Context, logger *logrus.Entry) {
Expand All @@ -253,20 +249,21 @@ func setFileHandler(c *gin.Context, logger *logrus.Entry) {
return
}

byteFiles, err := body.ByteFiles()
if err != nil {
logger.WithError(err).Warn("failed to get files from request")
c.Status(http.StatusBadRequest)
return
}
paths := make([]string, 0, len(byteFiles))
for _, byteFile := range byteFiles {
path := getTempFilePath(string(byteFile.Name))
if err := ioutil.WriteFile(path, byteFile.Bytes, 0644); err != nil {
logger.WithError(err).WithField("path", path).Warn("failed to create file")
paths := make([]string, 0, len(body.Files))
for _, file := range body.Files {
if file.Name == "-" && file.Base64 == "-" {
continue
}
path := getTempFilePath(file.Name)
fileBytes, err := file.Bytes()
if err != nil {
logger.WithField("filename", file.Name).Warn("failed to read file bytes")
continue
}
if err := ioutil.WriteFile(path, fileBytes, 0644); err != nil {
logger.WithError(err).WithField("path", path).Warn("failed to create fi le")
continue
}
logger.WithField("path", path).Debug()
paths = append(paths, path)
}
// write paths to file
Expand Down

0 comments on commit 9170de4

Please sign in to comment.