From 9edb886a157b0b994ccdd66af48ac63eea66cac9 Mon Sep 17 00:00:00 2001 From: Shane C Date: Sun, 17 Nov 2024 10:00:48 -0500 Subject: [PATCH] Initial commit --- .gitignore | 7 +++++++ .gitlab-ci.yml | 13 ------------ LICENSE | 10 +++++++++ file.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 19 +++++++++++++++++ go.sum | 28 +++++++++++++++++++++++++ storage.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 175 insertions(+), 13 deletions(-) create mode 100644 .gitignore delete mode 100644 .gitlab-ci.yml create mode 100644 LICENSE create mode 100644 file.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 storage.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8251c08 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# IDEs +.idea +.vscode +.fleet + +# Test Directory +test \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml deleted file mode 100644 index 195d65e..0000000 --- a/.gitlab-ci.yml +++ /dev/null @@ -1,13 +0,0 @@ -# You can override the included template(s) by including variable overrides -# SAST customization: https://docs.gitlab.com/ee/user/application_security/sast/#customizing-the-sast-settings -# Secret Detection customization: https://docs.gitlab.com/ee/user/application_security/secret_detection/pipeline/#customization -# Dependency Scanning customization: https://docs.gitlab.com/ee/user/application_security/dependency_scanning/#customizing-the-dependency-scanning-settings -# Container Scanning customization: https://docs.gitlab.com/ee/user/application_security/container_scanning/#customizing-the-container-scanning-settings -# Note that environment variables can be set in several places -# See https://docs.gitlab.com/ee/ci/variables/#cicd-variable-precedence -stages: -- test -sast: - stage: test -include: -- template: Security/SAST.gitlab-ci.yml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cde4ac6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,10 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. + +In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. + +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 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. + +For more information, please refer to diff --git a/file.go b/file.go new file mode 100644 index 0000000..9f6850b --- /dev/null +++ b/file.go @@ -0,0 +1,56 @@ +package fsys + +import ( + "bytes" + "context" + "github.com/minio/minio-go/v7" + "io" + "mime" + "os" + "path/filepath" +) + +type File struct { + storage *Storage + file io.ReadSeekCloser + + Name string +} + +func (f *File) Read(p []byte) (n int, err error) { + return f.file.Read(p) +} + +func (f *File) Seek(offset int64, whence int) (int64, error) { + return f.file.Seek(offset, whence) +} + +func (f *File) Write(p []byte) (n int, err error) { + if osFile, ok := f.file.(*os.File); ok { + return osFile.Write(p) + } else { + var fileContent []byte + if _, err := f.Read(fileContent); err != nil && err != io.EOF { + return 0, err + } + + fileContent = append(fileContent, p...) + + mimeType := mime.TypeByExtension(filepath.Ext(f.Name)) + if mimeType == "" { + mimeType = "application/octet-stream" + } + + _, err = f.storage.s3Client.PutObject(context.Background(), f.storage.config.S3BucketName, f.Name, bytes.NewBuffer(fileContent), int64(len(fileContent)), minio.PutObjectOptions{ + ContentType: mimeType, + }) + if err != nil { + return 0, err + } + return len(p), nil + } +} + +func (f *File) Close() error { + return f.file.Close() +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bb9e1a1 --- /dev/null +++ b/go.mod @@ -0,0 +1,19 @@ +module egtyl.xyz/omnibill/fsys + +go 1.23.2 + +require ( + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/go-ini/ini v1.67.0 // indirect + github.com/goccy/go-json v0.10.3 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/klauspost/compress v1.17.11 // indirect + github.com/klauspost/cpuid/v2 v2.2.8 // indirect + github.com/minio/md5-simd v1.1.2 // indirect + github.com/minio/minio-go/v7 v7.0.80 // indirect + github.com/rs/xid v1.6.0 // indirect + golang.org/x/crypto v0.28.0 // indirect + golang.org/x/net v0.30.0 // indirect + golang.org/x/sys v0.26.0 // indirect + golang.org/x/text v0.19.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..12e4fa7 --- /dev/null +++ b/go.sum @@ -0,0 +1,28 @@ +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= +github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= +github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= +github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc= +github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= +github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM= +github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= +github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= +github.com/minio/minio-go/v7 v7.0.80 h1:2mdUHXEykRdY/BigLt3Iuu1otL0JTogT0Nmltg0wujk= +github.com/minio/minio-go/v7 v7.0.80/go.mod h1:84gmIilaX4zcvAWWzJ5Z1WI5axN+hAbM5w25xf8xvC0= +github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU= +github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= +golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= +golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= +golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= +golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= +golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= +golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= diff --git a/storage.go b/storage.go new file mode 100644 index 0000000..2e723c1 --- /dev/null +++ b/storage.go @@ -0,0 +1,55 @@ +package fsys + +import ( + "errors" + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" +) + +var ( + ErrInvalidStorageType = errors.New("invalid storage type") + ErrInvalidPath = errors.New("invalid path") + ErrFileNotFound = errors.New("file not found") +) + +type Storage struct { + config *Config + s3Client *minio.Client +} + +type Config struct { + Type string + + Path string + + S3Endpoint string + S3Location string + S3Secure bool + + S3AccessID string + S3AccessKey string + + S3BucketName string +} + +func New(config *Config) (*Storage, error) { + newStorage := new(Storage) + newStorage.config = config + + switch config.Type { + case "minio", "s3": + s3Client, err := minio.New(config.S3Endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(config.S3AccessID, config.S3AccessKey, ""), + Secure: config.S3Secure, + }) + if err != nil { + return nil, err + } + newStorage.s3Client = s3Client + case "local": + default: + return nil, ErrInvalidStorageType + } + + return newStorage, nil +}