sunty
2020-08-20 9303b69ea569bcb5e581147543a3fd58e90d0d25
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
package fdfsclient
 
import (
    "fmt"
    "github.com/tedcy/fdfs_client"
    "sync"
)
 
var conf = "config/fdfs.conf"
var prifileExtName = "jpg"
 
func UploadFileByBuffer(bytes []byte, fileExtName string) string {
    client, err := fdfs_client.NewClientWithConfig(conf)
    defer client.Destory()
    if err != nil {
        fmt.Println(err.Error())
        return ""
    }
    if fileExtName == "" {
        fileExtName = prifileExtName
    }
    if fileId, err := client.UploadByBuffer(bytes, fileExtName); err != nil {
        fmt.Println(err.Error())
        return ""
    } else {
        fmt.Println(fileId)
        return fileId
    }
}
 
func UploadFileByBuffer100(bytess [][]byte, extNameList []string) (fields []string) {
    client, err := fdfs_client.NewClientWithConfig(conf)
    defer client.Destory()
    if err != nil {
        fmt.Println(err.Error())
        return nil
    }
    //var done = make(chan string)
    var wg sync.WaitGroup
    wg.Add(len(bytess))
     //ch := make(chan string,2)
    for _, bytes := range bytess {
        go func() {
            defer wg.Done()
            if fileld, err := client.UploadByBuffer(bytes,"jpg"); err != nil {
                fmt.Println(err.Error())
                fields = append(fields, "")
            } else {
                //ch <- fileld
                fields = append(fields,fileld)
                fmt.Println(fileld)
            }
        }()
    }
    wg.Wait()
    return fields
}
 
func downloadToBuffer(field string) (bytes []byte) {
    client, err := fdfs_client.NewClientWithConfig(conf)
    defer client.Destory()
    if err != nil {
        fmt.Println(err.Error())
        return nil
    }
    err1 := client.DownloadToAllocatedBuffer(field, bytes, 0, 0)
    if err1 != nil {
        fmt.Println(err1.Error())
        return nil
    } else {
        return bytes
    }
}