Skip to content

Commit

Permalink
refactor: update GetCustomerS3Zone to return ResponseCustomerS3 and a…
Browse files Browse the repository at this point in the history
…djust Upload logic
  • Loading branch information
allan committed Jan 23, 2025
1 parent eda3f90 commit 3ce649c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 58 deletions.
Empty file added .env.example
Empty file.
6 changes: 3 additions & 3 deletions compress/api_customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func (o *compress) GetCredentials() (*Credential, error) {
*
* @returns customer_s3
*/
func (o *compress) GetCustomerS3Zone() (*CustomerS3, error) {
func (o *compress) GetCustomerS3Zone() (*ResponseCustomerS3, error) {
//
resp, err := o.restyPost(GET_ZONE(), BaseModel{ClientId: o.GetCliendId(), ApiKey: o.apiKey} )
resp, err := o.restyPost(GET_ZONE(), BaseModel{ClientId: o.GetCliendId(), ApiKey: o.apiKey})
if err != nil {
return nil, err
}
//o.debugPrint(resp)
var obj CustomerS3
var obj ResponseCustomerS3
if err := json.Unmarshal(resp.Body(), &obj); err != nil {
return nil, err
}
Expand Down
48 changes: 15 additions & 33 deletions compress/api_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package compress
import (
"encoding/json"
"fmt"
"github.com/minio/minio-go"

"gopkg.in/validator.v2"
"io"
)

/**
Expand Down Expand Up @@ -153,8 +152,17 @@ func (o *compress) SetPublishedUpload(jobid int, published int) (*VideoUploadInf
* @param {string} targetFolder
*/
func (o *compress) Upload(file []byte, size int64, categoryId int, title string, tags string, location string, filename string, targetFolder string) (*ResponseUpload, error) {
// TODO: add logic get zone
bucketFolderDestination := targetFolder + "/" + filename
//
respCustomerS3, err := o.GetCustomerS3Zone()
if err != nil {
return nil, err
}
if respCustomerS3.Response != "OK" {
return nil, fmt.Errorf("error %s", respCustomerS3.Message)
}
zone := respCustomerS3.Data.Zone
bucketFolderDestination := respCustomerS3.Data.BucketUpload + "/" + targetFolder + "/" + filename
o.debugPrint("bucketFolderDestination " + respCustomerS3.Data.BucketUpload)
responsePresignedUrl, err := o.getMinioURL(bucketFolderDestination, o.customerName)
if err != nil {
return nil, err
Expand All @@ -173,7 +181,7 @@ func (o *compress) Upload(file []byte, size int64, categoryId int, title string,
return nil, fmt.Errorf("upload to s3 bucket failed!, err: %s", err.Error())
}

responseCreateUploadAndEncode, err := o.createUpload(o.apiKey, bucketFolderDestination, size, categoryId, title, tags, location, o.customerName)
responseCreateUploadAndEncode, err := o.createUpload(o.apiKey, bucketFolderDestination, size, categoryId, title, tags, location, o.customerName, zone)
if err != nil {
return nil, err
}
Expand All @@ -185,7 +193,7 @@ func (o *compress) Upload(file []byte, size int64, categoryId int, title string,
return responseCreateUploadAndEncode, nil
}

func (o *compress) createUpload(apikey string, bucketFolderDestination string, size int64, categoryId int, title string, tags string, location string, customer string) (*ResponseUpload, error) {
func (o *compress) createUpload(apikey string, bucketFolderDestination string, size int64, categoryId int, title string, tags string, location string, customer string, zone string) (*ResponseUpload, error) {
var ru ResponseUpload
_, err := o.restClient.R().
SetHeader("Content-Type", "application/json").
Expand All @@ -209,7 +217,7 @@ func (o *compress) getMinioURL(bucketFolderDestination string, customer string)
SetHeader("Content-Type", "application/json").
SetBody(&minioUploadPresignedByApikeyRequest{
//Customer: customer,
ApiKey: o.apiKey,
ApiKey: o.apiKey,
ClientId: o.clientId,
FileName: bucketFolderDestination,
}).
Expand All @@ -226,29 +234,3 @@ func (o *compress) getMinioURL(bucketFolderDestination string, customer string)

return responsePresignedUrl, nil
}

func (o *compress) UploadMultipart(reader io.Reader, size int64, categoryId int, title string, tags string, location string, filename string, targetFolder string) (*ResponseUpload, error) {
objectPath := targetFolder + "/" + filename
_, err := o.minioClient.PutObject(
o.bucket,
objectPath,
reader,
size,
minio.PutObjectOptions{},
)

if err != nil {
return nil, err
}

responseCreateUploadAndEncode, err := o.createUpload(o.apiKey, objectPath, size, categoryId, title, tags, location, o.customerName)
if err != nil {
return nil, err
}

if responseCreateUploadAndEncode.Response != "OK" {
return nil, fmt.Errorf("something went wrong during create upload and encode, err: %s %s", responseCreateUploadAndEncode.Message, responseCreateUploadAndEncode.Response)
}

return responseCreateUploadAndEncode, nil
}
21 changes: 2 additions & 19 deletions compress/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package compress

import (
"fmt"
"io"
"strings"

"github.com/go-resty/resty/v2"
Expand All @@ -19,7 +18,7 @@ type ICompress interface {
GetJobidProgress(jobid int) (*VideoUploadInfo, error)
SetPublishedUpload(jobid int, published int) (*VideoUploadInfo, error)
Upload(file []byte, size int64, categoryId int, title string, tags string, location string, filename string, targetFolder string) (*ResponseUpload, error)
UploadMultipart(reader io.Reader, size int64, categoryId int, title string, tags string, location string, filename string, targetFolder string) (*ResponseUpload, error)
//UploadMultipart(reader io.Reader, size int64, categoryId int, title string, tags string, location string, filename string, targetFolder string) (*ResponseUpload, error) // need to add this feature
GetCategories() ([]Category, error)
CreateCategory(name string) (*Category, error)
GetRestreamers(startFrom int, amount int) ([]Restreamer, error) // startFrom int, amount int
Expand All @@ -30,14 +29,13 @@ type ICompress interface {
RestreamerHlsStart(instanceName string, streamProtocol string) (*HlsResponse, error)
RestreamerHlsStop(instanceName string, streamProtocol string) (*HlsResponse, error)
RestreamerEventsHistory(startFrom int, amount int) ([]RestreamerEvent, error)
GetCustomerS3Zone() (*CustomerS3, error)
GetCustomerS3Zone() (*ResponseCustomerS3, error)
GenerateVodProxy(eventId string, instanceName string, title string) (*generateVodResponse, error)
//
}

type compress struct {
restClient *resty.Client
minioClient *minio.Client
bucket string
debug bool
customerName string
Expand Down Expand Up @@ -70,21 +68,6 @@ func NewCompress(clientId, apiKey string, isDebug bool) (ICompress, error) {
}
c.customerId = cred.CustomerID

/*u, err := url.Parse(cred.S3Host)
if err != nil {
return nil, err
}
// @p4xx07 need to refactor with multizone
c.minioClient, err = getMinioClient(
u.Host,
cred.S3AccessKey,
cred.S3SecretKey,
false,
)
c.bucket = cred.S3Bucket
if err != nil {
return nil, err
}*/
return c, nil
}

Expand Down
11 changes: 8 additions & 3 deletions compress/model_customer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package compress

import "time"

/*
*/
*/
type Credential struct {
ID int `json:"id"`
CustomerID int `json:"customer_id" `
Expand Down Expand Up @@ -34,13 +35,17 @@ type Credential struct {
LumenS3SecretKey string `json:"lumen_s3_secret_key" `
}


type ResponseServerCredential struct {
Message string `json:"message"`
Response string `json:"response"`
Data Credential `json:"data"`
}

type ResponseCustomerS3 struct {
Message string `json:"message"`
Response string `json:"response"`
Data CustomerS3 `json:"data"`
}

type CustomerS3 struct {
ID int `json:"id" `
Expand All @@ -63,4 +68,4 @@ type CustomerS3 struct {
VodUnprotectedBaseURL string `json:"vod_unprotected_base_url"`
Zone string `json:"zone"`
BucketUpload string `json:"bucket_upload"`
}
}
2 changes: 2 additions & 0 deletions test/api_upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestUpload(t *testing.T) {
}
}

/*
func TestUploadMultipart(t *testing.T) {
if os.Getenv("APP_ENV") == "runner" {
return
Expand Down Expand Up @@ -85,3 +86,4 @@ func TestUploadMultipart(t *testing.T) {
t.Fatalf(err.Error())
}
}
*/

0 comments on commit 3ce649c

Please sign in to comment.