diff --git a/pkg/data/audio.go b/pkg/data/audio.go index 32fe4f077..813670f96 100644 --- a/pkg/data/audio.go +++ b/pkg/data/audio.go @@ -46,19 +46,19 @@ var audioGetter = map[string]func(*audioData) (format.Value, error){ "aiff": func(a *audioData) (format.Value, error) { return a.Convert(AIFF) }, } -func NewAudioFromBytes(b []byte, contentType, fileName string) (*audioData, error) { - return createAudioData(b, contentType, fileName) +func NewAudioFromBytes(b []byte, contentType, filename string) (*audioData, error) { + return createAudioData(b, contentType, filename) } func NewAudioFromURL(url string) (*audioData, error) { - b, contentType, fileName, err := convertURLToBytes(url) + b, contentType, filename, err := convertURLToBytes(url) if err != nil { return nil, err } - return createAudioData(b, contentType, fileName) + return createAudioData(b, contentType, filename) } -func createAudioData(b []byte, contentType, fileName string) (*audioData, error) { +func createAudioData(b []byte, contentType, filename string) (*audioData, error) { if contentType != OGG { var err error b, err = convertAudio(b, contentType, OGG) @@ -68,7 +68,7 @@ func createAudioData(b []byte, contentType, fileName string) (*audioData, error) contentType = OGG } - f, err := NewFileFromBytes(b, contentType, fileName) + f, err := NewFileFromBytes(b, contentType, filename) if err != nil { return nil, err } diff --git a/pkg/data/document.go b/pkg/data/document.go index 4cb98350b..6821ad649 100644 --- a/pkg/data/document.go +++ b/pkg/data/document.go @@ -34,21 +34,21 @@ var documentGetters = map[string]func(*documentData) (format.Value, error){ func (documentData) IsValue() {} -func NewDocumentFromBytes(b []byte, contentType, fileName string) (*documentData, error) { - return createDocumentData(b, contentType, fileName) +func NewDocumentFromBytes(b []byte, contentType, filename string) (*documentData, error) { + return createDocumentData(b, contentType, filename) } func NewDocumentFromURL(url string) (*documentData, error) { - b, contentType, fileName, err := convertURLToBytes(url) + b, contentType, filename, err := convertURLToBytes(url) if err != nil { return nil, err } - return createDocumentData(b, contentType, fileName) + return createDocumentData(b, contentType, filename) } -func createDocumentData(b []byte, contentType, fileName string) (*documentData, error) { - f, err := NewFileFromBytes(b, contentType, fileName) +func createDocumentData(b []byte, contentType, filename string) (*documentData, error) { + f, err := NewFileFromBytes(b, contentType, filename) if err != nil { return nil, err } @@ -92,7 +92,7 @@ func (d *documentData) Text() (val format.String, err error) { res, err := transformer.ConvertDocumentToMarkdown( &transformer.ConvertDocumentToMarkdownTransformerInput{ Document: dataURI.String(), - Filename: d.fileName, + Filename: d.filename, }, transformer.GetMarkdownTransformer) if err != nil { return nil, err @@ -159,7 +159,7 @@ func (d *documentData) Images() (mp Array, err error) { } res, err := transformer.ConvertDocumentToImage(&transformer.ConvertDocumentToImagesTransformerInput{ Document: dataURI.String(), - Filename: d.fileName, + Filename: d.filename, }) if err != nil { return nil, err diff --git a/pkg/data/file.go b/pkg/data/file.go index 0373b50a3..fbc16dda0 100644 --- a/pkg/data/file.go +++ b/pkg/data/file.go @@ -21,7 +21,7 @@ const ( var fileGetters = map[string]func(*fileData) (format.Value, error){ "source-url": func(f *fileData) (format.Value, error) { return f.SourceURL(), nil }, - "filename": func(f *fileData) (format.Value, error) { return f.FileName(), nil }, + "filename": func(f *fileData) (format.Value, error) { return f.Filename(), nil }, "file-size": func(f *fileData) (format.Value, error) { return f.FileSize(), nil }, "content-type": func(f *fileData) (format.Value, error) { return f.ContentType(), nil }, "binary": func(f *fileData) (format.Value, error) { return f.Binary() }, @@ -32,13 +32,13 @@ var fileGetters = map[string]func(*fileData) (format.Value, error){ type fileData struct { raw []byte contentType string - fileName string + filename string sourceURL string } func (fileData) IsValue() {} -func NewFileFromBytes(b []byte, contentType, fileName string) (bin *fileData, err error) { +func NewFileFromBytes(b []byte, contentType, filename string) (bin *fileData, err error) { if contentType == "" { contentType = strings.Split(mimetype.Detect(b).String(), ";")[0] } @@ -46,13 +46,13 @@ func NewFileFromBytes(b []byte, contentType, fileName string) (bin *fileData, er f := &fileData{ raw: b, contentType: contentType, - fileName: fileName, + filename: filename, } return f, nil } -func convertURLToBytes(url string) (b []byte, contentType string, fileName string, err error) { +func convertURLToBytes(url string) (b []byte, contentType string, filename string, err error) { if strings.HasPrefix(url, "data:") { return convertDataURIToBytes(url) } @@ -67,25 +67,25 @@ func convertURLToBytes(url string) (b []byte, contentType string, fileName strin if headers := resp.Header().Get("Content-Type"); headers != "" { contentType = headers } - fileName = "" + filename = "" if disposition := resp.Header().Get("Content-Disposition"); disposition != "" { if strings.HasPrefix(disposition, "attachment") { if _, params, err := mime.ParseMediaType(disposition); err == nil { if fn, ok := params["filename"]; ok { - fileName = fn + filename = fn } } } } - return body, contentType, fileName, nil + return body, contentType, filename, nil } func NewFileFromURL(url string) (bin *fileData, err error) { - b, contentType, fileName, err := convertURLToBytes(url) + b, contentType, filename, err := convertURLToBytes(url) if err != nil { return nil, err } - bin, err = NewFileFromBytes(b, contentType, fileName) + bin, err = NewFileFromBytes(b, contentType, filename) if err != nil { return nil, err } @@ -93,12 +93,12 @@ func NewFileFromURL(url string) (bin *fileData, err error) { return bin, nil } -func convertDataURIToBytes(url string) (b []byte, contentType string, fileName string, err error) { - b, contentType, fileName, err = decodeDataURI(url) +func convertDataURIToBytes(url string) (b []byte, contentType string, filename string, err error) { + b, contentType, filename, err = decodeDataURI(url) if err != nil { return } - return b, contentType, fileName, nil + return b, contentType, filename, nil } func (f *fileData) String() string { @@ -147,8 +147,8 @@ func (f *fileData) ContentType() (t format.String) { return NewString(f.contentType) } -func (f *fileData) FileName() (t format.String) { - return NewString(f.fileName) +func (f *fileData) Filename() (t format.String) { + return NewString(f.filename) } func (f *fileData) SourceURL() (t format.String) { @@ -205,7 +205,7 @@ func (f *fileData) Equal(other format.Value) bool { } return bytes.Equal(f.raw, ba.ByteArray()) && f.contentType == other.ContentType().String() && - f.fileName == other.FileName().String() && + f.filename == other.Filename().String() && f.sourceURL == other.SourceURL().String() } return false diff --git a/pkg/data/format/format.go b/pkg/data/format/format.go index 6ce7b6024..6e8be501a 100644 --- a/pkg/data/format/format.go +++ b/pkg/data/format/format.go @@ -44,7 +44,7 @@ type File interface { Base64() (url String, err error) FileSize() (size Number) ContentType() (t String) - FileName() (t String) + Filename() (t String) SourceURL() (t String) String() string } diff --git a/pkg/data/image.go b/pkg/data/image.go index 83939f0e5..514d3cb69 100644 --- a/pkg/data/image.go +++ b/pkg/data/image.go @@ -46,27 +46,27 @@ var imageGetters = map[string]func(*imageData) (format.Value, error){ } // NewImageFromBytes creates a new imageData from byte slice -func NewImageFromBytes(b []byte, contentType, fileName string) (*imageData, error) { - return createImageData(b, contentType, fileName) +func NewImageFromBytes(b []byte, contentType, filename string) (*imageData, error) { + return createImageData(b, contentType, filename) } // NewImageFromURL creates a new imageData from a URL func NewImageFromURL(url string) (*imageData, error) { - b, contentType, fileName, err := convertURLToBytes(url) + b, contentType, filename, err := convertURLToBytes(url) if err != nil { return nil, err } - return createImageData(b, contentType, fileName) + return createImageData(b, contentType, filename) } // createImageData is a helper function to create imageData -func createImageData(b []byte, contentType, fileName string) (*imageData, error) { +func createImageData(b []byte, contentType, filename string) (*imageData, error) { b, err := convertImage(b, contentType, PNG) if err != nil { return nil, err } - f, err := NewFileFromBytes(b, PNG, fileName) + f, err := NewFileFromBytes(b, PNG, filename) if err != nil { return nil, err } diff --git a/pkg/data/utils.go b/pkg/data/utils.go index 34daf4047..a7f7db140 100644 --- a/pkg/data/utils.go +++ b/pkg/data/utils.go @@ -10,7 +10,7 @@ import ( "github.com/instill-ai/pipeline-backend/pkg/data/format" ) -func decodeDataURI(s string) (b []byte, contentType string, fileName string, err error) { +func decodeDataURI(s string) (b []byte, contentType string, filename string, err error) { slices := strings.Split(s, ",") if len(slices) == 1 { b, err = base64.StdEncoding.DecodeString(s) @@ -24,7 +24,7 @@ func decodeDataURI(s string) (b []byte, contentType string, fileName string, err key, value, _ := strings.Cut(tag, "=") if key == "filename" || key == "fileName" || key == "file-name" { - fileName = value + filename = value } } } @@ -52,27 +52,27 @@ func StandardizePath(path string) (newPath string, err error) { } return newPath, err } -func NewBinaryFromBytes(b []byte, contentType, fileName string) (format.Value, error) { +func NewBinaryFromBytes(b []byte, contentType, filename string) (format.Value, error) { if contentType == "" { contentType = strings.Split(mimetype.Detect(b).String(), ";")[0] } switch { case isImageContentType(contentType): - return NewImageFromBytes(b, contentType, fileName) + return NewImageFromBytes(b, contentType, filename) case isAudioContentType(contentType): - return NewAudioFromBytes(b, contentType, fileName) + return NewAudioFromBytes(b, contentType, filename) case isVideoContentType(contentType): - return NewVideoFromBytes(b, contentType, fileName) + return NewVideoFromBytes(b, contentType, filename) case isDocumentContentType(contentType): - return NewDocumentFromBytes(b, contentType, fileName) + return NewDocumentFromBytes(b, contentType, filename) default: - return NewFileFromBytes(b, contentType, fileName) + return NewFileFromBytes(b, contentType, filename) } } func NewBinaryFromURL(url string) (format.Value, error) { - b, contentType, fileName, err := convertURLToBytes(url) + b, contentType, filename, err := convertURLToBytes(url) if err != nil { return nil, err } @@ -83,15 +83,15 @@ func NewBinaryFromURL(url string) (format.Value, error) { switch { case isImageContentType(contentType): - return NewImageFromBytes(b, contentType, fileName) + return NewImageFromBytes(b, contentType, filename) case isAudioContentType(contentType): - return NewAudioFromBytes(b, contentType, fileName) + return NewAudioFromBytes(b, contentType, filename) case isVideoContentType(contentType): - return NewVideoFromBytes(b, contentType, fileName) + return NewVideoFromBytes(b, contentType, filename) case isDocumentContentType(contentType): - return NewDocumentFromBytes(b, contentType, fileName) + return NewDocumentFromBytes(b, contentType, filename) default: - return NewFileFromBytes(b, contentType, fileName) + return NewFileFromBytes(b, contentType, filename) } } diff --git a/pkg/data/video.go b/pkg/data/video.go index 5ac69a6db..212a1c111 100644 --- a/pkg/data/video.go +++ b/pkg/data/video.go @@ -48,19 +48,19 @@ var videoGetters = map[string]func(*videoData) (format.Value, error){ "webm": func(v *videoData) (format.Value, error) { return v.Convert(WEBM) }, } -func NewVideoFromBytes(b []byte, contentType, fileName string) (video *videoData, err error) { - return createVideoData(b, contentType, fileName) +func NewVideoFromBytes(b []byte, contentType, filename string) (video *videoData, err error) { + return createVideoData(b, contentType, filename) } func NewVideoFromURL(url string) (video *videoData, err error) { - b, contentType, fileName, err := convertURLToBytes(url) + b, contentType, filename, err := convertURLToBytes(url) if err != nil { return nil, err } - return createVideoData(b, contentType, fileName) + return createVideoData(b, contentType, filename) } -func createVideoData(b []byte, contentType, fileName string) (*videoData, error) { +func createVideoData(b []byte, contentType, filename string) (*videoData, error) { if contentType != MP4 { var err error b, err = convertVideo(b, contentType, MP4) @@ -69,7 +69,7 @@ func createVideoData(b []byte, contentType, fileName string) (*videoData, error) } contentType = MP4 } - f, err := NewFileFromBytes(b, contentType, fileName) + f, err := NewFileFromBytes(b, contentType, filename) if err != nil { return nil, err } diff --git a/pkg/data/video_test.go b/pkg/data/video_test.go index 7eaed46ab..a696d1699 100644 --- a/pkg/data/video_test.go +++ b/pkg/data/video_test.go @@ -16,7 +16,7 @@ func TestNewVideoFromBytes(t *testing.T) { testCases := []struct { name string - fileName string + filename string contentType string expectError bool }{ @@ -28,10 +28,10 @@ func TestNewVideoFromBytes(t *testing.T) { for _, tc := range testCases { c.Run(tc.name, func(c *qt.C) { - videoBytes, err := os.ReadFile(filepath.Join("testdata", tc.fileName)) + videoBytes, err := os.ReadFile(filepath.Join("testdata", tc.filename)) c.Assert(err, qt.IsNil) - video, err := NewVideoFromBytes(videoBytes, tc.contentType, tc.fileName) + video, err := NewVideoFromBytes(videoBytes, tc.contentType, tc.filename) if tc.expectError { c.Assert(err, qt.Not(qt.IsNil)) @@ -39,7 +39,7 @@ func TestNewVideoFromBytes(t *testing.T) { c.Assert(err, qt.IsNil) c.Assert(video, qt.Not(qt.IsNil)) c.Assert(video.ContentType().String(), qt.Equals, tc.contentType) - c.Assert(video.FileName().String(), qt.Equals, tc.fileName) + c.Assert(video.Filename().String(), qt.Equals, tc.filename) } }) } @@ -47,18 +47,18 @@ func TestNewVideoFromBytes(t *testing.T) { c.Run("Invalid video format", func(c *qt.C) { invalidBytes := []byte("not a video") contentType := "invalid/type" - fileName := "invalid.txt" + filename := "invalid.txt" - _, err := NewVideoFromBytes(invalidBytes, contentType, fileName) + _, err := NewVideoFromBytes(invalidBytes, contentType, filename) c.Assert(err, qt.Not(qt.IsNil)) }) c.Run("Empty video bytes", func(c *qt.C) { emptyBytes := []byte{} contentType := "video/mp4" - fileName := "empty.mp4" + filename := "empty.mp4" - _, err := NewVideoFromBytes(emptyBytes, contentType, fileName) + _, err := NewVideoFromBytes(emptyBytes, contentType, filename) c.Assert(err, qt.Not(qt.IsNil)) }) } @@ -98,7 +98,7 @@ func TestVideoProperties(t *testing.T) { testCases := []struct { name string - fileName string + filename string contentType string width int height int @@ -112,14 +112,14 @@ func TestVideoProperties(t *testing.T) { for _, tc := range testCases { c.Run(tc.name, func(c *qt.C) { - videoBytes, err := os.ReadFile(filepath.Join("testdata", tc.fileName)) + videoBytes, err := os.ReadFile(filepath.Join("testdata", tc.filename)) c.Assert(err, qt.IsNil) - video, err := NewVideoFromBytes(videoBytes, tc.contentType, tc.fileName) + video, err := NewVideoFromBytes(videoBytes, tc.contentType, tc.filename) c.Assert(err, qt.IsNil) qt.CmpEquals() c.Assert(video.ContentType().String(), qt.Equals, "video/mp4") - c.Assert(video.FileName().String(), qt.Equals, tc.fileName) + c.Assert(video.Filename().String(), qt.Equals, tc.filename) c.Assert(video.Width().Integer(), qt.Equals, tc.width) c.Assert(video.Height().Integer(), qt.Equals, tc.height) c.Assert(video.Duration().Float64(), qt.CmpEquals(cmpopts.EquateApprox(0, 0.001)), tc.duration) @@ -135,7 +135,7 @@ func TestVideoConvert(t *testing.T) { testCases := []struct { name string - fileName string + filename string contentType string expectedFormat string }{ @@ -146,10 +146,10 @@ func TestVideoConvert(t *testing.T) { for _, tc := range testCases { c.Run(tc.name, func(c *qt.C) { - videoBytes, err := os.ReadFile(filepath.Join("testdata", tc.fileName)) + videoBytes, err := os.ReadFile(filepath.Join("testdata", tc.filename)) c.Assert(err, qt.IsNil) - video, err := NewVideoFromBytes(videoBytes, tc.contentType, tc.fileName) + video, err := NewVideoFromBytes(videoBytes, tc.contentType, tc.filename) c.Assert(err, qt.IsNil) convertedVideo, err := video.Convert(tc.expectedFormat)