|
9 | 9 | "staticbackend/middleware"
|
10 | 10 | "time"
|
11 | 11 |
|
12 |
| - "github.com/aws/aws-sdk-go/aws" |
13 |
| - "github.com/aws/aws-sdk-go/aws/session" |
14 |
| - "github.com/aws/aws-sdk-go/service/s3" |
15 | 12 | "go.mongodb.org/mongo-driver/bson"
|
16 | 13 | "go.mongodb.org/mongo-driver/bson/primitive"
|
17 | 14 | )
|
@@ -52,36 +49,20 @@ func upload(w http.ResponseWriter, r *http.Request) {
|
52 | 49 | name = primitive.NewObjectID().Hex()
|
53 | 50 | }
|
54 | 51 |
|
55 |
| - sess, err := session.NewSession(&aws.Config{Region: aws.String("ca-central-1")}) |
56 |
| - if err != nil { |
57 |
| - http.Error(w, err.Error(), http.StatusInternalServerError) |
58 |
| - return |
59 |
| - } |
60 |
| - |
61 | 52 | fileKey := fmt.Sprintf("%s/%s/%s%s",
|
62 | 53 | auth.AccountID.Hex(),
|
63 | 54 | config.Name,
|
64 | 55 | name,
|
65 | 56 | ext,
|
66 | 57 | )
|
67 | 58 |
|
68 |
| - svc := s3.New(sess) |
69 |
| - obj := &s3.PutObjectInput{} |
70 |
| - obj.Body = file |
71 |
| - obj.ACL = aws.String(s3.ObjectCannedACLPublicRead) |
72 |
| - obj.Bucket = aws.String("files.staticbackend.com") |
73 |
| - obj.Key = aws.String(fileKey) |
74 |
| - |
75 |
| - if _, err := svc.PutObject(obj); err != nil { |
| 59 | + upData := internal.UploadFileData{FileKey: fileKey, File: file} |
| 60 | + url, err := storer.Save(upData) |
| 61 | + if err != nil { |
76 | 62 | http.Error(w, err.Error(), http.StatusInternalServerError)
|
77 | 63 | return
|
78 | 64 | }
|
79 | 65 |
|
80 |
| - url := fmt.Sprintf( |
81 |
| - "https://cdn.staticbackend.com/%s", |
82 |
| - fileKey, |
83 |
| - ) |
84 |
| - |
85 | 66 | doc := bson.M{
|
86 | 67 | "accountId": auth.AccountID,
|
87 | 68 | "key": fileKey,
|
@@ -148,18 +129,7 @@ func deleteFile(w http.ResponseWriter, r *http.Request) {
|
148 | 129 | return
|
149 | 130 | }
|
150 | 131 |
|
151 |
| - sess, err := session.NewSession(&aws.Config{Region: aws.String("ca-central-1")}) |
152 |
| - if err != nil { |
153 |
| - http.Error(w, err.Error(), http.StatusInternalServerError) |
154 |
| - return |
155 |
| - } |
156 |
| - |
157 |
| - svc := s3.New(sess) |
158 |
| - obj := &s3.DeleteObjectInput{ |
159 |
| - Bucket: aws.String("files.staticbackend.com"), |
160 |
| - Key: aws.String(fileKey), |
161 |
| - } |
162 |
| - if _, err := svc.DeleteObject(obj); err != nil { |
| 132 | + if err := storer.Delete(fileKey); err != nil { |
163 | 133 | http.Error(w, err.Error(), http.StatusInternalServerError)
|
164 | 134 | return
|
165 | 135 | }
|
|
0 commit comments