From 0e50805f32dc761889e62c5dc297bf148283c648 Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Thu, 23 Jan 2025 10:07:23 +0100 Subject: [PATCH 1/3] TODO: add new logic for v4 multizone --- compress/api_upload.go | 1 + 1 file changed, 1 insertion(+) diff --git a/compress/api_upload.go b/compress/api_upload.go index 971005a..4b2b85c 100644 --- a/compress/api_upload.go +++ b/compress/api_upload.go @@ -153,6 +153,7 @@ 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 responsePresignedUrl, err := o.getMinioURL(bucketFolderDestination, o.customerName) if err != nil { From eda3f908995c78fb0b9d17ce6867a989d6be947b Mon Sep 17 00:00:00 2001 From: allan Date: Thu, 23 Jan 2025 10:32:29 +0100 Subject: [PATCH 2/3] fix: correct usage of os.Getenv for CLIENT_ID in test --- test/a_main_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/a_main_test.go b/test/a_main_test.go index a486823..03f1db5 100644 --- a/test/a_main_test.go +++ b/test/a_main_test.go @@ -3,6 +3,7 @@ package test import ( "os" "testing" + "github.com/HiWay-Media/compress-go/compress" ) @@ -28,7 +29,7 @@ func TestNewCompress(t *testing.T) { func GetCompress() (compress.ICompress, error) { apiKey := os.Getenv("API_KEY") - clientId := os.GetEnv("CLIENT_ID") + clientId := os.Getenv("CLIENT_ID") // c, err := compress.NewCompress(clientId, apiKey, false) if err != nil { From 3ce649cd97a466c8154c33df452b6d30357d49cc Mon Sep 17 00:00:00 2001 From: allan Date: Thu, 23 Jan 2025 11:17:20 +0100 Subject: [PATCH 3/3] refactor: update GetCustomerS3Zone to return ResponseCustomerS3 and adjust Upload logic --- .env.example | 0 compress/api_customer.go | 6 ++--- compress/api_upload.go | 48 ++++++++++++-------------------------- compress/compress.go | 21 ++--------------- compress/model_customer.go | 11 ++++++--- test/api_upload_test.go | 2 ++ 6 files changed, 30 insertions(+), 58 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..e69de29 diff --git a/compress/api_customer.go b/compress/api_customer.go index 963421a..7fb93ee 100644 --- a/compress/api_customer.go +++ b/compress/api_customer.go @@ -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 } diff --git a/compress/api_upload.go b/compress/api_upload.go index 4b2b85c..93c312e 100644 --- a/compress/api_upload.go +++ b/compress/api_upload.go @@ -3,9 +3,8 @@ package compress import ( "encoding/json" "fmt" - "github.com/minio/minio-go" + "gopkg.in/validator.v2" - "io" ) /** @@ -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 @@ -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 } @@ -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"). @@ -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, }). @@ -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 -} diff --git a/compress/compress.go b/compress/compress.go index e1ce608..e717064 100644 --- a/compress/compress.go +++ b/compress/compress.go @@ -2,7 +2,6 @@ package compress import ( "fmt" - "io" "strings" "github.com/go-resty/resty/v2" @@ -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 @@ -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 @@ -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 } diff --git a/compress/model_customer.go b/compress/model_customer.go index 5d6136e..653defe 100644 --- a/compress/model_customer.go +++ b/compress/model_customer.go @@ -1,8 +1,9 @@ package compress import "time" + /* -*/ + */ type Credential struct { ID int `json:"id"` CustomerID int `json:"customer_id" ` @@ -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" ` @@ -63,4 +68,4 @@ type CustomerS3 struct { VodUnprotectedBaseURL string `json:"vod_unprotected_base_url"` Zone string `json:"zone"` BucketUpload string `json:"bucket_upload"` -} \ No newline at end of file +} diff --git a/test/api_upload_test.go b/test/api_upload_test.go index 4d4ad2e..417cf76 100644 --- a/test/api_upload_test.go +++ b/test/api_upload_test.go @@ -45,6 +45,7 @@ func TestUpload(t *testing.T) { } } +/* func TestUploadMultipart(t *testing.T) { if os.Getenv("APP_ENV") == "runner" { return @@ -85,3 +86,4 @@ func TestUploadMultipart(t *testing.T) { t.Fatalf(err.Error()) } } +*/