lijian 6 rokov pred
rodič
commit
d801c3c940

+ 54 - 3
services/fileaccess/fileaccess.go

@@ -1,23 +1,74 @@
 package main
 
 import (
+	"fmt"
+	"io"
+	"os"
+	"path/filepath"
+	"regexp"
 	"sparrow/pkg/rpcs"
 	"sparrow/pkg/server"
+	"sparrow/pkg/utils"
 	"sync"
 )
 
-// FileAccessor RPC服务
+// FileAccess RPC服务
 type FileAccess struct {
 	mu sync.RWMutex
 }
 
-// NewFileAccessor create a FileAccessor instance
+// 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 {
-	server.Log.Info("调用rpc")
+	// 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
 }

+ 1 - 1
services/fileaccess/flags.go

@@ -8,7 +8,7 @@ const (
 	flagAllowExt   = "allowext" //允许上传的文件格式
 	flagDomain     = "domain"   // 文件服务域名
 
-	defaultStaticPath = "./upload"
+	defaultStaticPath = "upload"
 	defaultMaxSize    = 300 << 10         //默认300K
 	defaultAllowExt   = ".jpeg|.jpg|.png" //注意.号
 )

+ 1 - 1
services/fileaccess/readme.markdown

@@ -1,5 +1,5 @@
 ## 实现一个简单文件服务器
-
+* 支持GZip
 * 上传的文件全部放到upload/tmp临时目录中
 * 执行保存操作后,把文件拷贝到正式的文件目录,并生成文件路径
 * 定时清理tmp目录中的文件(如24小时清理一次)

+ 0 - 25
services/registry/product.go

@@ -4,7 +4,6 @@ import (
 	"errors"
 	"fmt"
 	"sparrow/pkg/models"
-	"sparrow/pkg/rpcs"
 	"sparrow/pkg/server"
 )
 
@@ -67,30 +66,6 @@ func (r *Registry) DelProduct(product *models.Product, reply *models.Product) er
 	return nil
 }
 
-// GetProducts 获取当前用户的产品
-func (r *Registry) GetProducts(args *rpcs.ArgsProductList, reply map[string]interface{}) error {
-	db, err := getDB()
-	if err != nil {
-		return err
-	}
-	tx := db.Where("1=1")
-	if args.ProductName != "" {
-		tx = tx.Where("product_name like ?", "%"+args.ProductName+"%")
-	}
-	var datas []models.Product
-	var total int
-	err = tx.Limit(args.Ps).Offset((args.Pi - 1) * args.Ps).Find(&datas).Error
-	tx.Model(&models.Product{}).Count(&total)
-	if err != nil {
-		return err
-	}
-	reply = map[string]interface{}{
-		"list":  datas,
-		"total": total,
-	}
-	return nil
-}
-
 // FindProduct will find product by specified ID
 func (r *Registry) FindProduct(id int32, reply *models.Product) error {
 	db, err := getDB()