Skip to content

Commit

Permalink
Refactor interface StorageService: does not depend on any struct
Browse files Browse the repository at this point in the history
  • Loading branch information
minhduc140583 committed Aug 22, 2021
1 parent 1a8f036 commit 55ba098
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
10 changes: 9 additions & 1 deletion echo/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s FileHandler) UploadFile() echo.HandlerFunc {
bytes := bufferFile.Bytes()
contentTye := handler.Header.Get(contentTypeHeader)
if len(contentTye) == 0 {
contentTye = filepath.Ext(handler.Filename)
contentTye = getExt(handler.Filename)
}
rs, err2 := s.Service.Upload(r.Context(), s.Directory, handler.Filename, bytes, contentTye)
if err2 != nil {
Expand All @@ -90,3 +90,11 @@ func log(logError func(context.Context, string), r *http.Request, err string) {
logError(r.Context(), err)
}
}
func getExt(file string) string {
ext := filepath.Ext(file)
if strings.HasPrefix(ext, ":") {
ext = ext[1:]
return ext
}
return ext
}
10 changes: 9 additions & 1 deletion echo_v3/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s FileHandler) UploadFile() echo.HandlerFunc {
bytes := bufferFile.Bytes()
contentTye := handler.Header.Get(contentTypeHeader)
if len(contentTye) == 0 {
contentTye = filepath.Ext(handler.Filename)
contentTye = getExt(handler.Filename)
}
rs, err2 := s.Service.Upload(r.Context(), s.Directory, handler.Filename, bytes, contentTye)
if err2 != nil {
Expand All @@ -90,3 +90,11 @@ func log(logError func(context.Context, string), r *http.Request, err string) {
logError(r.Context(), err)
}
}
func getExt(file string) string {
ext := filepath.Ext(file)
if strings.HasPrefix(ext, ":") {
ext = ext[1:]
return ext
}
return ext
}
10 changes: 9 additions & 1 deletion gin/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (s FileHandler) UploadFile() gin.HandlerFunc {
bytes := bufferFile.Bytes()
contentTye := handler.Header.Get(contentTypeHeader)
if len(contentTye) == 0 {
contentTye = filepath.Ext(handler.Filename)
contentTye = getExt(handler.Filename)
}
rs, err2 := s.Service.Upload(r.Context(), s.Directory, handler.Filename, bytes, contentTye)
if err2 != nil {
Expand All @@ -94,3 +94,11 @@ func log(logError func(context.Context, string), r *http.Request, err string) {
logError(r.Context(), err)
}
}
func getExt(file string) string {
ext := filepath.Ext(file)
if strings.HasPrefix(ext, ":") {
ext = ext[1:]
return ext
}
return ext
}
10 changes: 9 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (s FileHandler) UploadFile(w http.ResponseWriter, r *http.Request) {
bytes := bufferFile.Bytes()
contentTye := handler.Header.Get(contentTypeHeader)
if len(contentTye) == 0 {
contentTye = filepath.Ext(handler.Filename)
contentTye = getExt(handler.Filename)
}
rs, err2 := s.Service.Upload(r.Context(), s.Directory, handler.Filename, bytes, contentTye)
if err2 != nil {
Expand All @@ -94,3 +94,11 @@ func log(logError func(context.Context, string), r *http.Request, err string) {
logError(r.Context(), err)
}
}
func getExt(file string) string {
ext := filepath.Ext(file)
if strings.HasPrefix(ext, ":") {
ext = ext[1:]
return ext
}
return ext
}

0 comments on commit 55ba098

Please sign in to comment.