Skip to content

Commit

Permalink
excluding certain files from storing
Browse files Browse the repository at this point in the history
  • Loading branch information
xis committed Aug 12, 2020
1 parent 4549c01 commit d0cbdeb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
15 changes: 13 additions & 2 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ type Process struct {
saver Saver
}

// Store calls a function from Saver interface to save files
// Store same with StoreWithout but uses default exclude parameter
// excludes json files by default from saving into disk
func (p *Process) Store(prefix string) error {
err := p.saver.Save(prefix, p.storage.path)
err := p.StoreWithout(prefix, "applicaton/json")
if err != nil {
return err
}
return nil
}

// StoreWithout calls a function from Saver interface to save files
// you can give a variadic parameter for exclude content types
func (p *Process) StoreWithout(prefix string, excludedContentTypes ...string) error {
err := p.saver.Save(prefix, p.storage.path, excludedContentTypes...)
if err != nil {
return err
}
Expand Down
38 changes: 33 additions & 5 deletions saver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package baraka
import (
"io"
"os"
"path/filepath"
"strconv"
"strings"
)

// Saver is a interface for determine which data type to store
// Parse interface functions returns this interface
type Saver interface {
Save(prefix string, path string) error
Save(prefix string, path string, excludedContentTypes ...string) error
}

// Parts implements the Saver interface
Expand All @@ -20,10 +22,19 @@ type Parts struct {

// Save is a method for saving multipart files.
// this method saves Parts data.
func (s *Parts) Save(prefix string, path string) error {
for i, header := range s.files {
f := header.Open()
out, err := os.Create(path + prefix + strconv.Itoa(i))
func (s *Parts) Save(prefix string, path string, excludedContentTypes ...string) error {
for key := range s.files {

fileHeader := s.files[key]
if len(excludedContentTypes) != 0 {
ok := isExcluded(fileHeader.Header.Get("Content-Type"), excludedContentTypes...)
if !ok {
continue
}
}
extension := filepath.Ext(fileHeader.Filename)
f := fileHeader.Open()
out, err := os.Create(path + prefix + strconv.Itoa(key) + extension)
defer out.Close()
if err != nil {
return err
Expand All @@ -35,3 +46,20 @@ func (s *Parts) Save(prefix string, path string) error {
}
return nil
}

// helper functions for saver

// isExcluded returns that if file's content type is on excluded content type list or not.
func isExcluded(contentType string, excludedContentTypes ...string) bool {
for key := range excludedContentTypes {
i := strings.Index(contentType, ";")
if i == -1 {
i = len(contentType)
}
contentType = strings.TrimSpace(strings.ToLower(contentType[0:i]))
if contentType == excludedContentTypes[key] {
return false
}
}
return true
}
4 changes: 4 additions & 0 deletions saver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ func TestSave_MultipartParts(t *testing.T) {
if err != nil && err.(*os.PathError).Err != syscall.ENOENT {
t.Error(err)
}
err = mp.Save("test", "./", "text/plain")
if err != nil {
t.Error(err)
}
}

0 comments on commit d0cbdeb

Please sign in to comment.