Skip to content

Commit

Permalink
Merge pull request #13 from nabbar/add_pkg_archive
Browse files Browse the repository at this point in the history
Add Package archive
  • Loading branch information
Nicolas JUHEL authored Jun 30, 2020
2 parents 120c9c4 + 097cd3f commit 3cba3a4
Show file tree
Hide file tree
Showing 9 changed files with 678 additions and 0 deletions.
92 changes: 92 additions & 0 deletions njs-archive/archive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* MIT License
*
* Copyright (c) 2020 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package njs_archive

import (
"os"

"github.com/nabbar/golib/njs-archive/bz2"
"github.com/nabbar/golib/njs-archive/gzip"
"github.com/nabbar/golib/njs-archive/tar"
"github.com/nabbar/golib/njs-archive/zip"

//. "github.com/nabbar/golib/njs-logger"

iou "github.com/nabbar/golib/njs-ioutils"
)

type ArchiveType uint8

func ExtractFile(src *os.File, fileNameContain, fileNameRegex string) (*os.File, error) {
var err error

loc := src.Name()

if dst, err := bz2.GetFile(src, fileNameContain, fileNameRegex); err == nil {
//DebugLevel.Log("try deleting source archive...")
if err = iou.DelTempFile(src, true); err != nil {
return nil, err
}
//DebugLevel.Log("try another archive...")
return ExtractFile(dst, fileNameContain, fileNameRegex)
}

if dst, err := gzip.GetFile(src, fileNameContain, fileNameRegex); err == nil {
//DebugLevel.Log("try deleting source archive...")
if err = iou.DelTempFile(src, true); err != nil {
return nil, err
}
//DebugLevel.Log("try another archive...")
return ExtractFile(dst, fileNameContain, fileNameRegex)
}

if dst, err := tar.GetFile(src, fileNameContain, fileNameRegex); err == nil {
//DebugLevel.Log("try deleting source archive...")
if err = iou.DelTempFile(src, true); err != nil {
return nil, err
}
//DebugLevel.Log("try another archive...")
return ExtractFile(dst, fileNameContain, fileNameRegex)
}

if dst, err := zip.GetFile(src, fileNameContain, fileNameRegex); err == nil {
//DebugLevel.Log("try deleting source archive...")
if err = iou.DelTempFile(src, true); err != nil {
return nil, err
}
//DebugLevel.Log("try another archive...")
return ExtractFile(dst, fileNameContain, fileNameRegex)
}

if _, err = src.Seek(0, 0); err != nil {
if src, err = os.Open(loc); err != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "reopening file", err)
return nil, err
}
}

return src, nil
}
94 changes: 94 additions & 0 deletions njs-archive/archive/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* MIT License
*
* Copyright (c) 2020 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package archive

import (
"path"
"regexp"
"strings"
)

type File struct {
Name string
Path string
}

func (a File) Matching(containt string) bool {
if len(containt) < 1 {
return false
}
return strings.Contains(strings.ToLower(a.Name), strings.ToLower(containt))
}

func (a File) Regex(regex string) bool {
if len(regex) < 1 {
return false
}

b, _ := regexp.MatchString(regex, a.Name)

return b
}

func (a File) MatchingFullPath(containt string) bool {
if len(containt) < 1 {
return false
}
return strings.Contains(strings.ToLower(a.GetKeyMap()), strings.ToLower(containt))
}

func (a File) RegexFullPath(regex string) bool {
if len(regex) < 1 {
return false
}

b, _ := regexp.MatchString(regex, a.GetKeyMap())

return b
}

func (a File) GetKeyMap() string {
return path.Join(a.Path, a.Name)
}

func (a File) GetDestFileOnly(baseDestination string) string {
return path.Join(baseDestination, a.Name)
}

func (a File) GetDestWithPath(baseDestination string) string {
return path.Join(baseDestination, a.Path, a.Name)
}

func NewFile(name, path string) File {
return File{
Name: name,
Path: path,
}
}

func NewFileFullPath(fullpath string) File {
return NewFile(path.Base(fullpath), path.Dir(fullpath))
}
58 changes: 58 additions & 0 deletions njs-archive/bz2/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* MIT License
*
* Copyright (c) 2020 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package bz2

import (
"compress/bzip2"
"io"
"os"

//. "github.com/nabbar/golib/njs-logger"

iou "github.com/nabbar/golib/njs-ioutils"
)

func GetFile(src *os.File, filenameContain, filenameRegex string) (dst *os.File, err error) {
if _, e := src.Seek(0, 0); e != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "seeking buffer", e)
return nil, e
}

r := bzip2.NewReader(src)

if t, e := iou.NewTempFile(); e != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "init new temporary buffer", e)
return nil, e
} else if _, e = io.Copy(t, r); e != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "copy buffer from archive reader", e)
return nil, e
} else if _, e = t.Seek(0, 0); e != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "seeking temp file", e)
return nil, e
} else {
return t, nil
}
}
64 changes: 64 additions & 0 deletions njs-archive/gzip/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* MIT License
*
* Copyright (c) 2020 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package gzip

import (
gz "compress/gzip"
"io"
"os"

//. "github.com/nabbar/golib/njs-logger"

iou "github.com/nabbar/golib/njs-ioutils"
)

func GetFile(src *os.File, filenameContain, filenameRegex string) (dst *os.File, err error) {
if _, e := src.Seek(0, 0); e != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "seeking buffer", e)
return nil, e
}

r, e := gz.NewReader(src)
if e != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "init gzip reader", e)
return nil, e
}

defer r.Close()

if t, e := iou.NewTempFile(); e != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "init new temporary buffer", e)
return nil, e
} else if _, e = io.Copy(t, r); e != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "copy buffer from archive reader", e)
return nil, e
} else if _, e = t.Seek(0, 0); e != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "seeking temp file", e)
return nil, e
} else {
return t, nil
}
}
78 changes: 78 additions & 0 deletions njs-archive/tar/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* MIT License
*
* Copyright (c) 2020 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package tar

import (
"archive/tar"
"io"
"os"

"github.com/nabbar/golib/njs-archive/archive"

//. "github.com/nabbar/golib/njs-logger"

iou "github.com/nabbar/golib/njs-ioutils"
)

func GetFile(src *os.File, filenameContain, filenameRegex string) (dst *os.File, err error) {
if _, e := src.Seek(0, 0); e != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "seeking buffer", e)
return nil, e
}

r := tar.NewReader(src)

for {
h, e := r.Next()

if e != nil && e == io.EOF {
return nil, nil
} else if e != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "trying to read next file", e)
return nil, e
}

if h.FileInfo().Mode()&os.ModeType == os.ModeType {
continue
}

f := archive.NewFileFullPath(h.Name)
if f.MatchingFullPath(filenameContain) || f.RegexFullPath(filenameRegex) {
if t, e := iou.NewTempFile(); e != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "init new temporary buffer", e)
return nil, e
} else if _, e = io.Copy(t, r); e != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "copy buffer from archive reader", e)
return nil, e
} else if _, e = t.Seek(0, 0); e != nil {
//ErrorLevel.LogErrorCtx(DebugLevel, "seeking temp file", e)
return nil, e
} else {
return t, nil
}
}
}
}
Loading

0 comments on commit 3cba3a4

Please sign in to comment.