-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
188fa52
commit 4e62934
Showing
22 changed files
with
602 additions
and
20 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 |
---|---|---|
|
@@ -16,3 +16,4 @@ | |
|
||
.idea | ||
/config/config.yml | ||
app.env |
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,32 @@ | ||
package infrastructure | ||
|
||
import ( | ||
"context" | ||
"github.com/cloudinary/cloudinary-go/v2" | ||
) | ||
|
||
type Cloudinary struct { | ||
*cloudinary.Cloudinary | ||
} | ||
|
||
func NewCloudinary() *Cloudinary { | ||
cld, _ := credentials() | ||
return &Cloudinary{ | ||
cld, | ||
} | ||
} | ||
|
||
func credentials() (*cloudinary.Cloudinary, context.Context) { | ||
// Add your Cloudinary credentials, set configuration parameter | ||
// Secure=true to return "https" URLs, and create a context | ||
//=================== | ||
cld, err := cloudinary.New() | ||
if err != nil { | ||
panic(err) | ||
} | ||
// CLOUDINARY_URL=cloudinary://API-Key:API-Secret@Cloud-name | ||
// CLOUDINARY_URL=cloudinary://oZ47iHrgrFQq4fe7ksKKlo7tg4A:991793784142871@dsr2xnaj7 | ||
cld.Config.URL.Secure = true | ||
ctx := context.Background() | ||
return cld, ctx | ||
} |
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
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,101 @@ | ||
package lib | ||
|
||
import ( | ||
"context" | ||
"erp/cmd/infrastructure" | ||
"erp/config" | ||
"fmt" | ||
"github.com/cloudinary/cloudinary-go/v2/api" | ||
"github.com/cloudinary/cloudinary-go/v2/api/admin" | ||
"github.com/cloudinary/cloudinary-go/v2/api/uploader" | ||
"go.uber.org/zap" | ||
"mime/multipart" | ||
) | ||
|
||
type CloudinaryRepository interface { | ||
UploadFileCloud(ctx context.Context, file *multipart.FileHeader) (resp *uploader.UploadResult, err error) | ||
GetAssetInfo(ctx context.Context) | ||
TransformImage(ctx context.Context) | ||
} | ||
|
||
type cloudinaryRepository struct { | ||
cld *infrastructure.Cloudinary | ||
config *config.Config | ||
logger *zap.Logger | ||
} | ||
|
||
func NewCloudinaryRepository(cld *infrastructure.Cloudinary, logger *zap.Logger, config *config.Config) CloudinaryRepository { | ||
return &cloudinaryRepository{ | ||
cld: cld, | ||
config: config, | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (r *cloudinaryRepository) UploadFileCloud(ctx context.Context, file *multipart.FileHeader) (resp *uploader.UploadResult, err error) { | ||
|
||
// SaveFile the cloudinary. | ||
// Set the asset's public ID and allow overwriting the asset with new versions | ||
resp, err = r.cld.Upload.Upload(ctx, file, uploader.UploadParams{ | ||
PublicID: r.config.Cloudinary.PublicId, | ||
UniqueFilename: api.Bool(false), | ||
Overwrite: api.Bool(true)}) | ||
if err != nil { | ||
fmt.Println("error") | ||
} | ||
|
||
return resp, err | ||
} | ||
|
||
func (r *cloudinaryRepository) GetAssetInfo(ctx context.Context) { | ||
// Get and use details of the cloudinary | ||
// ============================== | ||
resp, err := r.cld.Admin.Asset(ctx, admin.AssetParams{PublicID: r.config.Cloudinary.PublicId}) | ||
if err != nil { | ||
fmt.Println("error") | ||
} | ||
fmt.Println("****3. Get and use details of the cloudinary****\nDetailed response:\n", resp, "\n") | ||
|
||
// Assign tags to the uploaded cloudinary based on its width. Save the response to the update in the variable 'update_resp'. | ||
if resp.Width > 900 { | ||
updateResp, err := r.cld.Admin.UpdateAsset(ctx, admin.UpdateAssetParams{ | ||
PublicID: r.config.Cloudinary.PublicId, | ||
Tags: []string{"large"}}) | ||
if err != nil { | ||
fmt.Println("error") | ||
} else { | ||
// Log the new tag to the console. | ||
fmt.Println("New tag: ", updateResp.Tags, "\n") | ||
} | ||
} else { | ||
updateResp, err := r.cld.Admin.UpdateAsset(ctx, admin.UpdateAssetParams{ | ||
PublicID: r.config.Cloudinary.PublicId, | ||
Tags: []string{"small"}}) | ||
if err != nil { | ||
fmt.Println("error") | ||
} else { | ||
// Log the new tag to the console. | ||
fmt.Println("New tag: ", updateResp.Tags, "\n") | ||
} | ||
} | ||
|
||
} | ||
|
||
func (r *cloudinaryRepository) TransformImage(ctx context.Context) { | ||
// Instantiate an object for the asset with public ID "my_image" | ||
qs_img, err := r.cld.Image("quickstart_butterfly") | ||
if err != nil { | ||
fmt.Println("error") | ||
} | ||
|
||
// Add the transformation | ||
qs_img.Transformation = "r_max/e_sepia" | ||
|
||
// Generate and log the delivery URL | ||
new_url, err := qs_img.String() | ||
if err != nil { | ||
fmt.Println("error") | ||
} else { | ||
print("****4. Transform the image****\nTransfrmation URL: ", new_url, "\n") | ||
} | ||
} |
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
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
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,2 @@ | ||
ENV=local | ||
CLOUDINARY_URL=cloudinary://991793784142871:oZ47iHrgrFQq4fe7ksKKlo7tg4A@dsr2xnaj7 |
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,2 @@ | ||
ENV=*** | ||
CLOUDINARY_URL=cloudinary://*** |
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
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,21 @@ | ||
package domain | ||
|
||
import ( | ||
"encoding/json" | ||
uuid "github.com/satori/go.uuid" | ||
) | ||
|
||
type File struct { | ||
BaseModel | ||
FileName string `json:"file_name" gorm:"column:file_name;type:varchar(50);not null"` | ||
Path string `json:"path" gorm:"column:path;type:varchar(255);not null"` | ||
Size int64 `json:"size" gorm:"column:size;type:bigint;not null"` | ||
ExtensionName string `json:"type" gorm:"column:extension_name;type:varchar(10);not null"` | ||
Data json.RawMessage `json:"domain" gorm:"column:data;type:jsonb;" swaggertype:"string"` // save domain flexibly | ||
UserId uuid.UUID `json:"user_id" gorm:"column:user_id;type:uuid"` | ||
User User `json:"user" gorm:"foreignKey:UserId; constraint:OnUpdate:CASCADE,OnDelete:CASCADE"` | ||
} | ||
|
||
func (File) TableName() string { | ||
return "files" | ||
} |
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
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.