Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
daviidwang committed Nov 13, 2023
2 parents a6f621f + b55abe2 commit ad08ecf
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
8 changes: 4 additions & 4 deletions docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
因为很多原因, 我找不到一个非常好的工具能共享两个不同操作系统的设备的剪切板.
当我拷贝了一段文字, 我需要在另外一台电脑上粘贴, 这通常非常困难.

现在的工具通常只支持文字, 但是截图, 文件不支持. 同时, 他们通常要求设备必须要在同一个局域网, 可以互相访问.
而且他们往往收费.
现在的工具通常只支持文字, 但是截图, 文件不支持. 而且, 他们通常要求设备必须要在同一个局域网, 可以互相访问.
他们往往收费.

这很不好!

Expand All @@ -27,7 +27,7 @@

假如, 你有两台电脑, 一台电脑安装了Windows, 另外一台电脑安装了MacOS, 他们在同一个局域网中, 可以互相访问.

我们在MacOS启动GCopy, 它同时是客户端和服务端.
我们在MacOS启动GCopy, 它既是客户端也是服务端.

```
$ /path/to/gcopy.exe --role=server,client
Expand All @@ -47,7 +47,7 @@ The Server has started, start the clients:

可以看到GCopy已经启动, 并打印出了客户端启动命令.

我们在Windows上, 打开Powershell并输入上面打印出的命令并执行.
我们在Windows上, 打开cmd/powershell并输入上面打印出的命令并执行. 你可能需要修改一下命令, 比如给可执行文件加上`.exe`后缀, 或者修改gcopy的路径.

**注意:** 客户端命令中出现的`--server=192.168.137.186:3375`是服务端监听的地址, 它是GCopy自动发现的您设备的IP地址, `3375`是GCopy工作的端口号. 如果他们不好使, 您可以在启动服务端的时候, 通过手动指定`--listen=<ip:port>`来自定义服务端监听的地址.

Expand Down
3 changes: 3 additions & 0 deletions internal/host/darwin/darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func (h *HostClipboardManager) Get(cb *clipboard.Clipboard) error {
}
out = clipOutput(out)
out = bytes.TrimSuffix(out, []byte("\n"))
if file.IsDir(string(out)) {
return errors.New("folders are not supported")
}
cb.ContentFilePath = string(out)
contentHash, err = hash.HashFile(cb.ContentFilePath)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/host/windows/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func (h *HostClipboardManager) Get(cb *clipboard.Clipboard) error {
return err
}
out = clipOutput(out)
if file.IsDir(string(out)) {
return errors.New("folders are not supported")
}
cb.ContentFilePath = string(out)
contentHash, err = hash.HashFile(cb.ContentFilePath)
if err != nil {
Expand Down
14 changes: 6 additions & 8 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ type Server struct {
cb *clipboard.Clipboard
log *logrus.Entry
ps *pubsub.PubSub

mu sync.Mutex
}

var ContentFilePath string
Expand Down Expand Up @@ -103,16 +101,17 @@ func (s *Server) updateClipboardHandler(c *gin.Context) {
return
}

s.mu.Lock()
defer s.mu.Unlock()

// save the uploaded file
file, err := c.FormFile("f")
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.SaveUploadedFile(file, ContentFilePath)

if err := c.SaveUploadedFile(file, ContentFilePath); err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}

contentHash, err := hash.HashFile(ContentFilePath)
if err != nil {
Expand All @@ -132,11 +131,10 @@ func (s *Server) updateClipboardHandler(c *gin.Context) {
CopiedFileName: copiedFileName,
}
s.cb = &new
s.ps.Publish()

c.Header("X-Index", fmt.Sprintf("%v", s.cb.Index))
c.String(http.StatusOK, fmt.Sprintf("clipboard uploaded: %+v", s.cb))

s.ps.Publish()
}

func (s *Server) getClipboardHandler(c *gin.Context) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/utils/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ func CopyFile(src, dst string) (int64, error) {
nBytes, err := io.Copy(destination, source)
return nBytes, err
}

func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}

0 comments on commit ad08ecf

Please sign in to comment.