@@ -2,12 +2,15 @@ package s3
2
2
3
3
import (
4
4
"context"
5
+ "crypto/md5"
6
+ "encoding/hex"
5
7
"fmt"
6
8
"github.com/TBXark/sphere/log"
7
- "github.com/TBXark/sphere/storage/models"
8
9
"io"
9
10
"net/url"
11
+ "path"
10
12
"strings"
13
+ "time"
11
14
12
15
"github.com/minio/minio-go/v7"
13
16
"github.com/minio/minio-go/v7/pkg/credentials"
@@ -86,8 +89,7 @@ func (s *Client) ExtractKeyFromURLWithMode(uri string, strict bool) (string, err
86
89
}
87
90
return uri , nil
88
91
}
89
- path := strings .TrimPrefix (u .Path , "/" )
90
- parts := strings .SplitN (path , "/" , 2 )
92
+ parts := strings .SplitN (strings .TrimPrefix (u .Path , "/" ), "/" , 2 )
91
93
if len (parts ) != 2 || parts [0 ] != s .config .Bucket {
92
94
if strict {
93
95
return "" , fmt .Errorf ("invalid url" )
@@ -97,24 +99,41 @@ func (s *Client) ExtractKeyFromURLWithMode(uri string, strict bool) (string, err
97
99
return parts [1 ], nil
98
100
}
99
101
100
- func (s * Client ) UploadFile (ctx context.Context , file io.Reader , size int64 , key string ) (* models.FileUploadResult , error ) {
101
- info , err := s .client .PutObject (ctx , s .config .Bucket , key , file , size , minio.PutObjectOptions {})
102
+ func (s * Client ) GenerateUploadToken (fileName string , dir string , nameBuilder func (filename string , dir ... string ) string ) ([3 ]string , error ) {
103
+ fileExt := path .Ext (fileName )
104
+ sum := md5 .Sum ([]byte (fileName ))
105
+ nameMd5 := hex .EncodeToString (sum [:])
106
+ key := nameBuilder (nameMd5 + fileExt , dir )
107
+ key = strings .TrimPrefix (key , "/" )
108
+
109
+ preSignedURL , err := s .client .PresignedPutObject (context .Background (),
110
+ s .config .Bucket ,
111
+ key ,
112
+ time .Hour )
102
113
if err != nil {
103
- return nil , err
114
+ return [ 3 ] string {} , err
104
115
}
105
- return & models.FileUploadResult {
106
- Key : info .Key ,
116
+ return [3 ]string {
117
+ preSignedURL .String (),
118
+ key ,
119
+ s .GenerateURL (key ),
107
120
}, nil
108
121
}
109
122
110
- func (s * Client ) UploadLocalFile (ctx context.Context , file string , key string ) (* models.FileUploadResult , error ) {
123
+ func (s * Client ) UploadFile (ctx context.Context , file io.Reader , size int64 , key string ) (string , error ) {
124
+ info , err := s .client .PutObject (ctx , s .config .Bucket , key , file , size , minio.PutObjectOptions {})
125
+ if err != nil {
126
+ return "" , err
127
+ }
128
+ return info .Key , nil
129
+ }
130
+
131
+ func (s * Client ) UploadLocalFile (ctx context.Context , file string , key string ) (string , error ) {
111
132
info , err := s .client .FPutObject (ctx , s .config .Bucket , key , file , minio.PutObjectOptions {})
112
133
if err != nil {
113
- return nil , err
134
+ return "" , err
114
135
}
115
- return & models.FileUploadResult {
116
- Key : info .Key ,
117
- }, nil
136
+ return info .Key , nil
118
137
}
119
138
120
139
func (s * Client ) DownloadFile (ctx context.Context , key string ) (io.ReadCloser , error ) {
0 commit comments