This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from kaduartur/feature/stream
[FEATURE] Move the fileutil to stream package
- Loading branch information
Showing
3 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package stream | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type DirCreater interface { | ||
Create(dir string) error | ||
} | ||
|
||
type DirRemover interface { | ||
Remove(dir string) error | ||
} | ||
|
||
// CreateDir implements DirCreater | ||
type CreateDir struct { | ||
} | ||
|
||
// NewDirCreater returns a CreateDir | ||
func NewDirCreater() CreateDir { | ||
return CreateDir{} | ||
} | ||
|
||
// Create creates a directory named dir | ||
// A successful call returns err == nil | ||
func (c CreateDir) Create(dir string) error { | ||
if err := os.MkdirAll(dir, os.ModePerm); err != nil { | ||
return fmt.Errorf("failed to create directory: '%s', error: '%s'", dir, err.Error()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// RemoveDir implements DirRemover | ||
type RemoveDir struct { | ||
} | ||
|
||
// NewDirRemover returns a RemoveDir | ||
func NewDirRemover() RemoveDir { | ||
return RemoveDir{} | ||
} | ||
|
||
// Remove removes dir and any children it contains. | ||
func (r RemoveDir) Remove(dir string) error { | ||
if err := os.RemoveAll(dir); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package stream | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
type FileExister interface { | ||
Exists(path string) bool | ||
} | ||
|
||
type FileReader interface { | ||
Read(path string) ([]byte, error) | ||
} | ||
|
||
type FileWriter interface { | ||
Write(path string, content []byte) error | ||
} | ||
|
||
type FileRemover interface { | ||
Remove(path string) error | ||
} | ||
|
||
type FileReadExister interface { | ||
FileReader | ||
FileExister | ||
} | ||
|
||
type FileWriteReadExister interface { | ||
FileWriter | ||
FileReader | ||
FileExister | ||
} | ||
|
||
type FileWriteReadExistRemover interface { | ||
FileWriter | ||
FileReader | ||
FileExister | ||
FileRemover | ||
} | ||
|
||
// FileManager implements FileWriteReadExistRemover | ||
type FileManager struct { | ||
} | ||
|
||
// NewFileManager returns a FileManage that writes from w | ||
// reads from r, exists from e and removes from re | ||
func NewFileManager() FileManager { | ||
return FileManager{} | ||
} | ||
|
||
// Read reads the file named by path and returns the contents. | ||
// A successful call returns err == nil | ||
func (f FileManager) Read(path string) ([]byte, error) { | ||
b, err := ioutil.ReadFile(path) | ||
if err != nil && !os.IsNotExist(err) { | ||
return nil, err | ||
} | ||
|
||
return b, err | ||
} | ||
|
||
// Exists returns true if file path exists | ||
func (f FileManager) Exists(path string) bool { | ||
if _, err := os.Stat(path); os.IsNotExist(err) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
// Write writes content to a file named by path. | ||
// A successful call returns err == nil | ||
func (f FileManager) Write(path string, content []byte) error { | ||
return ioutil.WriteFile(path, content, os.ModePerm) | ||
} | ||
|
||
// Remove removes the named file | ||
func (f FileManager) Remove(path string) error { | ||
if f.Exists(path) { | ||
return os.Remove(path) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package streams | ||
|
||
import ( | ||
"archive/zip" | ||
"io" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// Unzip wrapper for archive.zip | ||
func Unzip(src string, dest string) error { | ||
reader, _ := zip.OpenReader(src) | ||
for _, file := range reader.Reader.File { | ||
|
||
zippedFile, err := file.Open() | ||
if err != nil { | ||
return err | ||
} | ||
defer zippedFile.Close() | ||
|
||
extractedFilePath := filepath.Join( | ||
dest, | ||
file.Name, | ||
) | ||
|
||
if file.FileInfo().IsDir() { | ||
log.Println("Directory Created:", extractedFilePath) | ||
if err := os.MkdirAll(extractedFilePath, file.Mode()); err != nil { | ||
return err | ||
} | ||
} else { | ||
log.Println("File extracted:", file.Name) | ||
|
||
outputFile, err := os.OpenFile( | ||
extractedFilePath, | ||
os.O_WRONLY|os.O_CREATE|os.O_TRUNC, | ||
file.Mode(), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
defer outputFile.Close() | ||
|
||
_, err = io.Copy(outputFile, zippedFile) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
defer reader.Close() | ||
return nil | ||
} |