|   1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
 | func (t *HandlerTask) uploadFiles(c *gin.Context) {
	result := &common.Result{}
	req := model.UploadFileReq{}
	c.ShouldBind(&req)
	multipartForm, err := c.MultipartForm()
	if err != nil {
		zap.L().Error("c.MultipartForm() err", zap.Error(err))
		return
	}
	file := multipartForm.File
	key := ""
	minioClient, err := mio.New(
		"localhost:9009",
		"0RnUy2AUBZrpsI3U",
		"9lHE5HpEUwHni1vxaFnf9BosILz79nd3",
		false)
	if err != nil {
		c.JSON(http.StatusOK, result.Fail(-999, err.Error()))
		return
	}
	if req.TotalChunks == 1 {
		header := file["file"][0]
		open, err := header.Open()
		defer open.Close()
		buf := make([]byte, req.TotalSize)
		open.Read(buf)
		info, err := minioClient.Upload(
			context.Background(),
			"test",
			req.Filename,
			header.Header.Get("Content-Type"),
			buf,
		)
		if err != nil {
			c.JSON(http.StatusOK, result.Fail(-999, err.Error()))
		}
		key = info.Bucket + "/" + req.Filename
	}
	if req.TotalChunks > 1 {
		buf := make([]byte, req.CurrentChunkSize)
		//分片上传 合起来即可
		header := file["file"][0]
		open, _ := header.Open()
		defer open.Close()
		open.Read(buf)
		formatInt := strconv.FormatInt(int64(req.ChunkNumber), 10)
		info, err := minioClient.Upload(
			context.Background(),
			"test",
			req.Filename+"_"+formatInt,
			header.Header.Get("Content-Type"),
			buf,
		)
		if err != nil {
			c.JSON(http.StatusOK, result.Fail(-999, err.Error()))
		}
		key = info.Bucket + "/" + req.Filename
		if req.TotalChunks == req.ChunkNumber {
			err := minioClient.Compose(
				context.Background(),
				"test",
				req.Filename,
				header.Header.Get("Content-Type"),
				req.TotalChunks,
			)
			if err != nil {
				c.JSON(http.StatusOK, result.Fail(-999, err.Error()))
			}
		}
	}
	//调用服务 存入file表
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()
	fileUrl := "http://localhost:9009/" + key
	msg := &task.TaskFileReqMessage{
		TaskCode:         req.TaskCode,
		ProjectCode:      req.ProjectCode,
		OrganizationCode: c.GetString("organizationCode"),
		PathName:         key,
		FileName:         req.Filename,
		Size:             int64(req.TotalSize),
		Extension:        path.Ext(key),
		FileUrl:          fileUrl,
		FileType:         file["file"][0].Header.Get("Content-Type"),
		MemberId:         c.GetInt64("memberId"),
	}
	if req.TotalChunks == req.ChunkNumber {
		_, err = TaskServiceClient.SaveTaskFile(ctx, msg)
		if err != nil {
			code, msg := errs.ParseGrpcError(err)
			c.JSON(http.StatusOK, result.Fail(code, msg))
		}
	}
	c.JSON(http.StatusOK, result.Success(gin.H{
		"file":        key,
		"hash":        "",
		"key":         key,
		"url":         "http://localhost:9009/" + key,
		"projectName": req.ProjectName,
	}))
	return
}
 |