Skip to content

Commit

Permalink
Merge FileInfo and Message struct to reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
mstmdev committed Dec 23, 2021
1 parent 99ea2bf commit e0d2b6c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 32 deletions.
6 changes: 4 additions & 2 deletions contract/file_info.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package contract

type FileInfo struct {
// Path file path
// Path the file path
Path string `json:"path"`
// IsDir is a dir the path
IsDir bool `json:"is_dir"`
IsDir FsDirValue `json:"is_dir"`
// Size the size of path for bytes
Size int64 `json:"size"`
// Hash calculate the path hash value, if the path is a file
Hash string `json:"hash"`
// CTime create time, unix sec
CTime int64 `json:"c_time"`
// ATime last access time, unix sec
Expand Down
4 changes: 2 additions & 2 deletions monitor/remote_client_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ func (m *remoteClientMonitor) processingMessage() {
log.Debug("received other message, ignore it => %s", string(message.data))
} else {
values := url.Values{}
values.Add(contract.FsDir, util.String(msg.IsDir))
values.Add(contract.FsDir, msg.IsDir.String())
values.Add(contract.FsSize, util.String(msg.Size))
values.Add(contract.FsHash, util.String(msg.Hash))
values.Add(contract.FsHash, msg.Hash)
values.Add(contract.FsCtime, util.String(msg.CTime))
values.Add(contract.FsAtime, util.String(msg.ATime))
values.Add(contract.FsMtime, util.String(msg.MTime))
Expand Down
2 changes: 1 addition & 1 deletion server/handler/file_api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (h *fileApiHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
}
fileList = append(fileList, contract.FileInfo{
Path: file.Name(),
IsDir: file.IsDir(),
IsDir: contract.ParseFsDirValue(file.IsDir()),
Size: file.Size(),
ATime: aTime.Unix(),
CTime: cTime.Unix(),
Expand Down
15 changes: 1 addition & 14 deletions sync/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,9 @@ import "github.com/no-src/gofs/contract"
// Message a message of the remote file change
type Message struct {
contract.Status
// Path the file path
Path string `json:"path"`
contract.FileInfo
// Action the action of file change
Action Action `json:"action"`
// BaseUrl the base url of file server
BaseUrl string `json:"base_url"`
// IsDir is a dir the path
IsDir contract.FsDirValue `json:"is_dir"`
// Size the size of path for bytes
Size int64 `json:"size"`
// Hash calculate the path hash value, if the path is a file
Hash string `json:"hash"`
// CTime create time, unix sec
CTime int64 `json:"c_time"`
// ATime last access time, unix sec
ATime int64 `json:"a_time"`
// MTime last modify time, unix sec
MTime int64 `json:"m_time"`
}
8 changes: 2 additions & 6 deletions sync/remote_client_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,14 @@ func (rs *remoteClientSync) sync(serverAddr, path string) error {
}
for _, file := range files {
currentPath := path + "/" + file.Path
isDir := contract.FsNotDir
if file.IsDir {
isDir = contract.FsIsDir
}
values := url.Values{}
values.Add(contract.FsDir, util.String(isDir))
values.Add(contract.FsDir, file.IsDir.String())
values.Add(contract.FsSize, util.String(file.Size))
values.Add(contract.FsCtime, util.String(file.CTime))
values.Add(contract.FsAtime, util.String(file.ATime))
values.Add(contract.FsMtime, util.String(file.MTime))
syncPath := fmt.Sprintf("%s/%s?%s", serverAddr, currentPath, values.Encode())
if file.IsDir {
if file.IsDir.Bool() {
// create directory
rs.Create(syncPath)
// sync current directory content
Expand Down
16 changes: 9 additions & 7 deletions sync/remote_server_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,16 @@ func (rs *remoteServerSync) send(action Action, path string) (err error) {
req := Message{
Status: contract.SuccessStatus(contract.SyncMessageApi),
Action: action,
Path: path,
BaseUrl: rs.serverAddr + server.SrcRoutePrefix,
IsDir: isDirValue,
Size: size,
Hash: hash,
CTime: cTime.Unix(),
ATime: aTime.Unix(),
MTime: mTime.Unix(),
FileInfo: contract.FileInfo{
Path: path,
IsDir: isDirValue,
Size: size,
Hash: hash,
CTime: cTime.Unix(),
ATime: aTime.Unix(),
MTime: mTime.Unix(),
},
}

data, err := util.Marshal(req)
Expand Down

0 comments on commit e0d2b6c

Please sign in to comment.