|
@@ -1,11 +1,54 @@
|
|
|
package api
|
|
|
|
|
|
-import "github.com/gin-gonic/gin"
|
|
|
+import (
|
|
|
+ "cr-svc/db/model"
|
|
|
+ "cr-svc/db/repo"
|
|
|
+ "fmt"
|
|
|
+ "path/filepath"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
+)
|
|
|
|
|
|
func Upload(r *GinRouter) {
|
|
|
- r.POSTJWT("/upload/pkg", UploadPkg)
|
|
|
+ r.POST("/upload/pkg", UploadPkg)
|
|
|
}
|
|
|
|
|
|
+var LocalHost = "http://localhost:8085/cr/pkg"
|
|
|
+
|
|
|
+// type UploadReq struct {
|
|
|
+// Version string
|
|
|
+// Name string
|
|
|
+// }
|
|
|
+
|
|
|
func UploadPkg(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
- return nil, nil
|
|
|
+
|
|
|
+ version := c.PostForm("version")
|
|
|
+ name := c.PostForm("name")
|
|
|
+ userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
|
|
|
+ file, err := c.FormFile("file")
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ size := fmt.Sprintf("%.2fM", float64(file.Size)/float64(1024*1024))
|
|
|
+ fmt.Println(size)
|
|
|
+
|
|
|
+ basePath := "./package/"
|
|
|
+ filename := basePath + filepath.Base(file.Filename)
|
|
|
+ if err := c.SaveUploadedFile(file, filename); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ versions := &model.Versions{
|
|
|
+ UserId: userId,
|
|
|
+ UserName: apictx.User.Name,
|
|
|
+ Name: name,
|
|
|
+ Url: fmt.Sprintf("%s/%s", LocalHost, file.Filename),
|
|
|
+ Size: size,
|
|
|
+ Version: version,
|
|
|
+ CreateTime: time.Now(),
|
|
|
+ UpdateTime: time.Now(),
|
|
|
+ }
|
|
|
+ return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionVersions, versions)
|
|
|
}
|