Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #76 from kaduartur/feature/stream
Browse files Browse the repository at this point in the history
[FEATURE] Move the fileutil to stream package
  • Loading branch information
Marcos Guimarães authored May 20, 2020
2 parents 85cc3a5 + 25a65f2 commit 7afd83f
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
50 changes: 50 additions & 0 deletions pkg/stream/dir.go
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
}
84 changes: 84 additions & 0 deletions pkg/stream/file.go
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
}
53 changes: 53 additions & 0 deletions pkg/stream/streams/stream.go
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
}

0 comments on commit 7afd83f

Please sign in to comment.