-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from kimthu09/quoc
dev: upload file
- Loading branch information
Showing
16 changed files
with
365 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
PORT=8080 | ||
GO_ENV=dev | ||
DEBUG=false | ||
|
||
STATIC_PATH="/data/static" | ||
SERVER_HOST="http://localhost:8080" | ||
|
||
DB_USERNAME=root | ||
DB_PASSWORD=123456 | ||
DB_DATABASE=bookstoremanagement | ||
DB_HOST=bsm-mysql:3306 | ||
|
||
FILE_LOGGING=false | ||
SECRET_KEY=123456789 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
book-store-management-backend/component/uploadprovider/provider.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package uploadprovider | ||
|
||
import "book-store-management-backend/common" | ||
|
||
type UploadStaticProvider interface { | ||
UploadImage(data []byte, path string) (common.Image, error) | ||
} |
74 changes: 74 additions & 0 deletions
74
book-store-management-backend/component/uploadprovider/provider_firebasestorage.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package uploadprovider | ||
|
||
import ( | ||
"book-store-management-backend/common" | ||
"cloud.google.com/go/storage" | ||
"context" | ||
firebase "firebase.google.com/go/v4" | ||
"google.golang.org/api/option" | ||
"log" | ||
) | ||
|
||
type UploadProvider interface { | ||
UploadImage(ctx context.Context, data []byte, dst string) (*common.Image, error) | ||
SaveFileUploaded(ctx context.Context, data []byte, dst string) (*common.Image, error) | ||
} | ||
|
||
type firebaseStorageUploadProvider struct { | ||
storageBucketUri string | ||
keyFilePath string | ||
firebaseApp *firebase.App | ||
} | ||
|
||
func NewFirebaseStorageUploadProvider(bucketUri string, keyFilePath string) *firebaseStorageUploadProvider { | ||
provider := &firebaseStorageUploadProvider{ | ||
storageBucketUri: bucketUri, | ||
keyFilePath: keyFilePath, | ||
} | ||
|
||
config := &firebase.Config{ | ||
StorageBucket: bucketUri, | ||
} | ||
opt := option.WithCredentialsFile(keyFilePath) | ||
|
||
app, err := firebase.NewApp(context.Background(), config, opt) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
provider.firebaseApp = app | ||
|
||
return provider | ||
} | ||
|
||
func (provider *firebaseStorageUploadProvider) UploadImage(ctx context.Context, data []byte, dst string) (string, error) { | ||
app := provider.firebaseApp | ||
|
||
client, err := app.Storage(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
bucket, err := client.DefaultBucket() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Upload an object with storage.Writer. | ||
wc := bucket.Object(dst).NewWriter(ctx) | ||
if _, err = wc.Write(data); err != nil { | ||
return "", err | ||
} | ||
|
||
if err := wc.Close(); err != nil { | ||
return "", err | ||
} | ||
|
||
// Provide a public read ACL so the image can be accessed by anyone | ||
bucket.Object(dst).ACL().Set(ctx, storage.AllUsers, storage.RoleReader) | ||
|
||
attrs, err := bucket.Object(dst).Attrs(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
return attrs.MediaLink, nil | ||
} |
39 changes: 39 additions & 0 deletions
39
book-store-management-backend/component/uploadprovider/provider_static.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package uploadprovider | ||
|
||
import ( | ||
"book-store-management-backend/common" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type staticUploadProvider struct { | ||
staticPath string | ||
} | ||
|
||
func NewStaticUploadProvider(staticPath string) *staticUploadProvider { | ||
return &staticUploadProvider{ | ||
staticPath: staticPath, | ||
} | ||
} | ||
|
||
func (provider *staticUploadProvider) UploadImage(data []byte, filename string) (common.Image, error) { | ||
image := common.Image{ | ||
CloudName: "local", | ||
Url: "/" + filename, | ||
} | ||
|
||
// create file | ||
fullPath := filepath.Join(provider.staticPath, filename) | ||
file, err := os.Create(fullPath) | ||
if err != nil { | ||
return common.Image{}, err // Return the error if file creation fails | ||
} | ||
defer file.Close() | ||
|
||
// write data to file | ||
if _, err := file.Write(data); err != nil { | ||
return common.Image{}, err | ||
} | ||
|
||
return image, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/bash | ||
mkdir -p ./storage | ||
chmod 777 -R ./storage | ||
gin --appPort 8080 --immediate | ||
chmod 777 -R ./storage | ||
gin --appPort 8080 --immediate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.