Skip to content

Commit

Permalink
Feature/upload mp (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: Paxx <[email protected]>
  • Loading branch information
p4xx07 and Paxx authored Nov 17, 2023
1 parent 4c2caee commit c74cb11
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 11 deletions.
31 changes: 29 additions & 2 deletions compress/api_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package compress
import (
"encoding/json"
"fmt"

"github.com/minio/minio-go"
"gopkg.in/validator.v2"
"io"
)

/**
Expand All @@ -20,7 +21,7 @@ import (
* @param {string} tags
* @param {string} location_place
* @param {number} category_id
*/
*/
func (o *compress) GetUploads(uploadsPaginated UploadsPaginated) ([]VideoUploadInfo, error) {
//
if errs := validator.Validate(uploadsPaginated); errs != nil {
Expand Down Expand Up @@ -222,3 +223,29 @@ 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
}
43 changes: 35 additions & 8 deletions compress/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package compress

import (
"fmt"

"github.com/go-resty/resty/v2"
"github.com/minio/minio-go"
"io"
"net/url"
)

type ICompress interface {
Expand All @@ -16,6 +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)
GetCategories() ([]Category, error)
CreateCategory(name string) (*Category, error)
GetRestreamers() ([]Restreamer, error) // startFrom int, amount int
Expand All @@ -26,11 +29,13 @@ type ICompress interface {
}

type compress struct {
restClient *resty.Client
debug bool
customerName string
apiKey string
customerId int
restClient *resty.Client
minioClient *minio.Client
bucket string
debug bool
customerName string
apiKey string
customerId int
}

func NewCompress(customerName, apiKey string, isDebug bool) (ICompress, error) {
Expand All @@ -54,7 +59,29 @@ func NewCompress(customerName, apiKey string, isDebug bool) (ICompress, error) {
return nil, err
}
c.customerId = cred.CustomerID
//

u, err := url.Parse(cred.S3Host)
if err != nil {
return nil, err
}
c.minioClient, err = getMinioClient(
u.Host,
cred.S3AccessKey,
cred.S3SecretKey,
false,
)
c.bucket = cred.S3Bucket
if err != nil {
return nil, err
}
return c, nil
}
//

func getMinioClient(endpoint, accessKeyID, secretAccessKey string, useSSL bool) (*minio.Client, error) {
minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, false)
if err != nil {
return nil, err
}

return minioClient, nil
}
12 changes: 11 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ go 1.18

require (
github.com/go-resty/resty/v2 v2.10.0
github.com/minio/minio-go v6.0.14+incompatible
gopkg.in/validator.v2 v2.0.1
)

require golang.org/x/net v0.17.0 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
)
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-resty/resty/v2 v2.10.0 h1:Qla4W/+TMmv0fOeeRqzEpXPLfTUnR5HZ1+lGs+CkiCo=
github.com/go-resty/resty/v2 v2.10.0/go.mod h1:iiP/OpA0CkcL3IGt1O0+/SIItFUbkkyw5BGXiVdTu+A=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/minio/minio-go v6.0.14+incompatible h1:fnV+GD28LeqdN6vT2XdGKW8Qe/IfjJDswNVuni6km9o=
github.com/minio/minio-go v6.0.14+incompatible/go.mod h1:7guKYtitv8dktvNUGrhzmNlA5wrAABTQXCoesZdFQO8=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
Expand All @@ -25,6 +40,7 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand All @@ -36,6 +52,7 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand All @@ -44,6 +61,9 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/validator.v2 v2.0.1 h1:xF0KWyGWXm/LM2G1TrEjqOu4pa6coO9AlWSf3msVfDY=
gopkg.in/validator.v2 v2.0.1/go.mod h1:lIUZBlB3Im4s/eYp39Ry/wkR02yOPhZ9IwIRBjuPuG8=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
41 changes: 41 additions & 0 deletions test/api_upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,44 @@ func TestUpload(t *testing.T) {
t.Fatalf(err.Error())
}
}

func TestUploadMultipart(t *testing.T) {
if os.Getenv("APP_ENV") == "runner" {
return
}
c, err := GetCompress()
if err != nil {
t.Fatalf(err.Error())
}
c.IsDebug()

filePath := "test.mp4"
if err != nil {
t.Fatalf(err.Error())
}

stat, err := os.Stat(filePath)
if err != nil {
t.Fatalf(err.Error())
}

file, err := os.Open(filePath)
if err != nil {
t.Fatalf(err.Error())
}

_, err = c.UploadMultipart(
file,
stat.Size(),
60678,
"title",
"tag",
"",
"titlemultipart.mp4",
"/VMFS1/FILES/upload/test",
)

if err != nil {
t.Fatalf(err.Error())
}
}

0 comments on commit c74cb11

Please sign in to comment.