Skip to content

Commit

Permalink
chore: use filename instead of fileName (#815)
Browse files Browse the repository at this point in the history
Because

- we want to standardize naming conventions across the codebase by using
lowercase `filename` instead of camelCase `fileName`.

This commit

- replaces all instances of `fileName` with `filename`.
  • Loading branch information
donch1989 authored Nov 5, 2024
1 parent a946cfd commit 2cc986b
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 72 deletions.
12 changes: 6 additions & 6 deletions pkg/data/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/data/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 16 additions & 16 deletions pkg/data/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
Expand All @@ -32,27 +32,27 @@ 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]
}

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)
}
Expand All @@ -67,38 +67,38 @@ 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
}
bin.sourceURL = url
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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/data/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/data/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
28 changes: 14 additions & 14 deletions pkg/data/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/data/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit 2cc986b

Please sign in to comment.