package main import ( "fmt" "io" "os" "path/filepath" "regexp" "sparrow/pkg/rpcs" "sparrow/pkg/server" "sparrow/pkg/utils" "sync" ) // FileAccess RPC服务 type FileAccess struct { mu sync.RWMutex } // NewFileAccess create a FileAccessor instance func NewFileAccess() *FileAccess { return &FileAccess{} } // MoveFile move a file to new path // source:http://192.168.175.60:9000/upload/tmp/2c9d7d85-2266-450a-9d47-28e67703d818.jpeg func (f *FileAccess) MoveFile(args *rpcs.ArgsMoveFile, reply *rpcs.ReplyMoveFile) error { // check source file reg := regexp.MustCompile(`tmp/\$*.*`) src := reg.FindString(args.Source) fileName := filepath.Base(src) server.Log.Debug(src) src = *conStaticPath + "/" + src b, err := utils.Exists(src) if err != nil { server.Log.Error(err) return err } if b { // copy file sourceFileStat, err := os.Stat(src) if err != nil { return err } if !sourceFileStat.Mode().IsRegular() { return fmt.Errorf("%s is not a regular file", src) } source, err := os.Open(src) if err != nil { return err } defer source.Close() dst := *conStaticPath + "/" + args.Target + "/" + fileName utils.CreateIfNotExist(dst) destination, err := os.Create(dst) if err != nil { return err } defer destination.Close() io.Copy(destination, source) fpath := fmt.Sprintf("http://%s/%s/%s/%s", server.GetHTTPHost(), *conStaticPath, args.Target, fileName) if *conDomain != "" { fpath = fmt.Sprintf("http://%s/%s/%s/%s", *conDomain, *conStaticPath, args.Target, fileName) } reply.FilePath = fpath //delete src os.Remove(src) return nil } return nil }