mirror of
https://github.com/nabbar/golib.git
synced 2025-12-24 11:51:02 +08:00
Refactor package errors + packages names :
- Refactor ErrorType, list errors managment, codeError - Add interface Error with error interface implement - Add type CodeError assign typiclly to const that represent code of error - Add func to registry func to retrieve message from an uint16 codeError (typicaly a switch of each codeError const) - Add default errorCode with default errorMessage if no one code or message is found - Add modeError to manage how to manage compatibility between Error interface and error interface - Add Error interface that allow parent link (parent as error or Error interface), code and trace management - Add trace finder to allow find func/file/line caller when Error is call - Add http 2 transport in httpcli - Add http 2 transport in httpserver - Add function to get client http with timeout management in httpcli - Add function to get Error if occurs of http client in httpcli - Add test for smtp package - Chg return error by returning Error in all packages - Chg package njs-archive by archive - Chg package njs-certif by certificates - Chg package njs-console by console - Chg package njs-crypt by crypt - Chg package njs-errors by errors - Chg package njs-httpcli by httpcli - Chg package njs-httpserver by httpserver - Chg package njs-ioutils by ioutils - Chg package njs-ldap by ldap - Chg package njs-logger by logger - Chg package njs-password by password - Chg package njs-progress by progress - Chg package njs-router by router - Chg package njs-semaphore by semaphore - Chg package njs-smtp by smtp - Chg package njs-static by static - Chg package njs-status by status - Chg package njs-version by version - Fix dependancies gopkg by github/go-ldap for go module compatibility - Fix gin Abort call by gin Abort with Error in static package - Fix issue #18 in status package : replace partner by component - Fix go vet error - Del deprecated function - Del useless function & files - Bump dependancies - Apply CHG in README.md
This commit is contained in:
98
archive/archive.go
Normal file
98
archive/archive.go
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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 (
|
||||
"os"
|
||||
|
||||
"github.com/nabbar/golib/archive/bz2"
|
||||
"github.com/nabbar/golib/archive/gzip"
|
||||
"github.com/nabbar/golib/archive/tar"
|
||||
"github.com/nabbar/golib/archive/zip"
|
||||
|
||||
. "github.com/nabbar/golib/errors"
|
||||
//. "github.com/nabbar/golib/logger"
|
||||
|
||||
iou "github.com/nabbar/golib/ioutils"
|
||||
)
|
||||
|
||||
type ArchiveType uint8
|
||||
|
||||
func ExtractFile(src *os.File, fileNameContain, fileNameRegex string) (*os.File, 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); 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); 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); 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); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//DebugLevel.Log("try another archive...")
|
||||
return ExtractFile(dst, fileNameContain, fileNameRegex)
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
)
|
||||
|
||||
if _, err = src.Seek(0, 0); err != nil {
|
||||
e1 := FILE_SEEK.ErrorParent(err)
|
||||
if src, err = os.Open(loc); err != nil {
|
||||
//ErrorLevel.LogErrorCtx(DebugLevel, "reopening file", err)
|
||||
e2 := FILE_OPEN.ErrorParent(err)
|
||||
e2.AddParentError(e1)
|
||||
return nil, e2
|
||||
}
|
||||
}
|
||||
|
||||
return src, nil
|
||||
}
|
||||
94
archive/archive/model.go
Normal file
94
archive/archive/model.go
Normal file
@@ -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))
|
||||
}
|
||||
52
archive/bz2/error.go
Normal file
52
archive/bz2/error.go
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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 errors "github.com/nabbar/golib/errors"
|
||||
|
||||
const (
|
||||
EMPTY_PARAMS errors.CodeError = iota + errors.MIN_PKG_Archive + 10
|
||||
FILE_SEEK
|
||||
IO_COPY
|
||||
)
|
||||
|
||||
func init() {
|
||||
errors.RegisterFctMessage(getMessage)
|
||||
}
|
||||
|
||||
func getMessage(code errors.CodeError) (message string) {
|
||||
switch code {
|
||||
case EMPTY_PARAMS:
|
||||
return "given parameters is empty"
|
||||
case FILE_SEEK:
|
||||
return "cannot seek into file"
|
||||
case IO_COPY:
|
||||
return "io copy occurs error"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
59
archive/bz2/reader.go
Normal file
59
archive/bz2/reader.go
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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/errors"
|
||||
//. "github.com/nabbar/golib/logger"
|
||||
|
||||
iou "github.com/nabbar/golib/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, FILE_SEEK.ErrorParent(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, IO_COPY.ErrorParent(e)
|
||||
} else if _, e = t.Seek(0, 0); e != nil {
|
||||
//ErrorLevel.LogErrorCtx(DebugLevel, "seeking temp file", e)
|
||||
return nil, FILE_SEEK.ErrorParent(e)
|
||||
} else {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
52
archive/error.go
Normal file
52
archive/error.go
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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 errors "github.com/nabbar/golib/errors"
|
||||
|
||||
const (
|
||||
EMPTY_PARAMS errors.CodeError = iota + errors.MIN_PKG_Archive
|
||||
FILE_SEEK
|
||||
FILE_OPEN
|
||||
)
|
||||
|
||||
func init() {
|
||||
errors.RegisterFctMessage(getMessage)
|
||||
}
|
||||
|
||||
func getMessage(code errors.CodeError) (message string) {
|
||||
switch code {
|
||||
case EMPTY_PARAMS:
|
||||
return "given parameters is empty"
|
||||
case FILE_SEEK:
|
||||
return "cannot seek into file"
|
||||
case FILE_OPEN:
|
||||
return "cannot open file"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
55
archive/gzip/error.go
Normal file
55
archive/gzip/error.go
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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 errors "github.com/nabbar/golib/errors"
|
||||
|
||||
const (
|
||||
EMPTY_PARAMS errors.CodeError = iota + errors.MIN_PKG_Archive + 20
|
||||
GZ_READER
|
||||
FILE_SEEK
|
||||
IO_COPY
|
||||
)
|
||||
|
||||
func init() {
|
||||
errors.RegisterFctMessage(getMessage)
|
||||
}
|
||||
|
||||
func getMessage(code errors.CodeError) (message string) {
|
||||
switch code {
|
||||
case EMPTY_PARAMS:
|
||||
return "given parameters is empty"
|
||||
case GZ_READER:
|
||||
return "cannot create new reader GZip"
|
||||
case FILE_SEEK:
|
||||
return "cannot seek into file"
|
||||
case IO_COPY:
|
||||
return "io copy occurs error"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
65
archive/gzip/reader.go
Normal file
65
archive/gzip/reader.go
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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/errors"
|
||||
//. "github.com/nabbar/golib/logger"
|
||||
|
||||
iou "github.com/nabbar/golib/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, FILE_SEEK.ErrorParent(e)
|
||||
}
|
||||
|
||||
r, e := gz.NewReader(src)
|
||||
if e != nil {
|
||||
//ErrorLevel.LogErrorCtx(DebugLevel, "init gzip reader", e)
|
||||
return nil, GZ_READER.ErrorParent(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, IO_COPY.ErrorParent(e)
|
||||
} else if _, e := t.Seek(0, 0); e != nil {
|
||||
//ErrorLevel.LogErrorCtx(DebugLevel, "seeking temp file", e)
|
||||
return nil, FILE_SEEK.ErrorParent(e)
|
||||
} else {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
55
archive/tar/error.go
Normal file
55
archive/tar/error.go
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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 errors "github.com/nabbar/golib/errors"
|
||||
|
||||
const (
|
||||
EMPTY_PARAMS errors.CodeError = iota + errors.MIN_PKG_Archive + 30
|
||||
TAR_NEXT
|
||||
FILE_SEEK
|
||||
IO_COPY
|
||||
)
|
||||
|
||||
func init() {
|
||||
errors.RegisterFctMessage(getMessage)
|
||||
}
|
||||
|
||||
func getMessage(code errors.CodeError) (message string) {
|
||||
switch code {
|
||||
case EMPTY_PARAMS:
|
||||
return "given parameters is empty"
|
||||
case TAR_NEXT:
|
||||
return "cannot get next tar file"
|
||||
case FILE_SEEK:
|
||||
return "cannot seek into file"
|
||||
case IO_COPY:
|
||||
return "io copy occurs error"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
79
archive/tar/reader.go
Normal file
79
archive/tar/reader.go
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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/archive/archive"
|
||||
|
||||
. "github.com/nabbar/golib/errors"
|
||||
//. "github.com/nabbar/golib/logger"
|
||||
|
||||
iou "github.com/nabbar/golib/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, FILE_SEEK.ErrorParent(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, TAR_NEXT.ErrorParent(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, IO_COPY.ErrorParent(e)
|
||||
} else if _, e := t.Seek(0, 0); e != nil {
|
||||
//ErrorLevel.LogErrorCtx(DebugLevel, "seeking temp file", e)
|
||||
return nil, FILE_SEEK.ErrorParent(e)
|
||||
} else {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
61
archive/zip/error.go
Normal file
61
archive/zip/error.go
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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 zip
|
||||
|
||||
import errors "github.com/nabbar/golib/errors"
|
||||
|
||||
const (
|
||||
EMPTY_PARAMS errors.CodeError = iota + errors.MIN_PKG_Archive + 40
|
||||
FILE_OPEN
|
||||
FILE_CLOSE
|
||||
FILE_SEEK
|
||||
IO_COPY
|
||||
ZIP_OPEN
|
||||
)
|
||||
|
||||
func init() {
|
||||
errors.RegisterFctMessage(getMessage)
|
||||
}
|
||||
|
||||
func getMessage(code errors.CodeError) (message string) {
|
||||
switch code {
|
||||
case EMPTY_PARAMS:
|
||||
return "given parameters is empty"
|
||||
case FILE_OPEN:
|
||||
return "cannot open zipped file"
|
||||
case FILE_CLOSE:
|
||||
return "closing file occurs error"
|
||||
case FILE_SEEK:
|
||||
return "cannot seek into file"
|
||||
case IO_COPY:
|
||||
return "io copy occurs error"
|
||||
case ZIP_OPEN:
|
||||
return "cannot open zip file"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
107
archive/zip/reader.go
Normal file
107
archive/zip/reader.go
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* 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 zip
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
. "github.com/nabbar/golib/errors"
|
||||
//. "github.com/nabbar/golib/logger"
|
||||
|
||||
iou "github.com/nabbar/golib/ioutils"
|
||||
|
||||
"github.com/nabbar/golib/archive/archive"
|
||||
)
|
||||
|
||||
func GetFile(src *os.File, filenameContain, filenameRegex string) (dst *os.File, err Error) {
|
||||
location := iou.GetTempFilePath(src)
|
||||
|
||||
if e := src.Close(); err != nil {
|
||||
//ErrorLevel.LogErrorCtx(DebugLevel, "trying to close temp file", err)
|
||||
return dst, FILE_CLOSE.ErrorParent(e)
|
||||
}
|
||||
|
||||
return getFile(location, filenameContain, filenameRegex)
|
||||
}
|
||||
|
||||
func getFile(src string, filenameContain, filenameRegex string) (dst *os.File, err Error) {
|
||||
var (
|
||||
r *zip.ReadCloser
|
||||
e error
|
||||
)
|
||||
|
||||
if r, e = zip.OpenReader(src); e != nil {
|
||||
//ErrorLevel.LogErrorCtx(DebugLevel, "trying to open zip file", e)
|
||||
return nil, ZIP_OPEN.ErrorParent(e)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = r.Close()
|
||||
}()
|
||||
|
||||
for _, f := range r.File {
|
||||
if f.Mode()&os.ModeType == os.ModeType {
|
||||
continue
|
||||
}
|
||||
|
||||
z := archive.NewFileFullPath(f.Name)
|
||||
if z.MatchingFullPath(filenameContain) || z.RegexFullPath(filenameRegex) {
|
||||
return extratFile(f)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func extratFile(f *zip.File) (dst *os.File, err Error) {
|
||||
var (
|
||||
r io.ReadCloser
|
||||
e error
|
||||
)
|
||||
|
||||
if r, e = f.Open(); e != nil {
|
||||
//ErrorLevel.LogErrorCtx(DebugLevel, "open zipped file reader", err)
|
||||
return dst, FILE_OPEN.ErrorParent(e)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = r.Close()
|
||||
}()
|
||||
|
||||
if dst, err = iou.NewTempFile(); err != nil {
|
||||
//ErrorLevel.LogErrorCtx(DebugLevel, "init new temporary buffer", err)
|
||||
return
|
||||
} else if _, e = io.Copy(dst, r); e != nil {
|
||||
//ErrorLevel.LogErrorCtx(DebugLevel, "copy buffer from archive reader", err)
|
||||
return dst, IO_COPY.ErrorParent(e)
|
||||
}
|
||||
|
||||
_, e = dst.Seek(0, 0)
|
||||
//ErrorLevel.LogErrorCtx(DebugLevel, "seeking temp file", err)
|
||||
return dst, FILE_SEEK.ErrorParent(e)
|
||||
}
|
||||
Reference in New Issue
Block a user