From 444249e5c59f33b4d88ae593a532b40919d93c6c Mon Sep 17 00:00:00 2001 From: ahuigo <1781999+ahuigo@users.noreply.github.com> Date: Sat, 13 Jul 2024 00:08:30 +0800 Subject: [PATCH] feat(file): generate file header --- .github/workflows/test.yml | 28 ++++++++++++++++++ .gitignore | 3 ++ LICENSE | 21 ++++++++++++++ file/file-header.go | 59 ++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ makefile | 28 ++++++++++++++++++ readme.md | 10 +++++++ version | 1 + 8 files changed, 153 insertions(+) create mode 100644 .github/workflows/test.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 file/file-header.go create mode 100644 go.mod create mode 100644 makefile create mode 100644 readme.md create mode 100644 version diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..9982bd0 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,28 @@ +name: Tests + +on: + push: + tags: + branches: + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.21 + stable: false + - name: Test + run: make test + + - name: Codecov + uses: codecov/codecov-action@v3 + with: + token: ${{ secrets.CODECOV_TOKEN }} + file: ./cover.out + flags: unittests + verbose: true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac51cd5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +cover.out +/tmp +__debug_bin* diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1e4d80e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Ahuigo + +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. diff --git a/file/file-header.go b/file/file-header.go new file mode 100644 index 0000000..bd7036f --- /dev/null +++ b/file/file-header.go @@ -0,0 +1,59 @@ +package file + +import ( + "bytes" + "errors" + "io" + "mime" + "mime/multipart" + "os" + "path/filepath" +) + +func CreateFileHeaderFromFile(filePath string) (*multipart.FileHeader, error) { + filename := filepath.Base(filePath) + file, err := os.Open(filePath) + if err != nil { + return nil, err + } + defer file.Close() + if content, err := io.ReadAll(file); err == nil { + return CreateFileHeaderFromBytes(filename, content) + }else{ + return nil, err + } +} + +func CreateFileHeaderFromBytes(filename string, content []byte) (*multipart.FileHeader, error) { + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + part, err := writer.CreateFormFile("file1", filename) + if err != nil { + return nil, err + } + + _, err = io.Copy(part, bytes.NewReader(content)) + if err != nil { + return nil, err + } + err = writer.Close() + if err != nil { + return nil, err + } + + _, params, err := mime.ParseMediaType(writer.FormDataContentType()) + if err != nil { + return nil, err + } + + boundary, ok := params["boundary"] + if !ok { + return nil, errors.New("no boundary") + } + + reader := multipart.NewReader(body, boundary) + mf, _ := reader.ReadForm(1 << 8) + fileHeader := mf.File["file1"][0] + + return fileHeader, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..83bf0f6 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/ahuigo/gohttptool + +go 1.22.1 diff --git a/makefile b/makefile new file mode 100644 index 0000000..73919d4 --- /dev/null +++ b/makefile @@ -0,0 +1,28 @@ +msg?= + +######################### test ################ +test: + go test -race -coverprofile cover.out -coverpkg "./..." -failfast ./... +cover: test + go tool cover -html=cover.out +race: + go test -race -failfast ./... +fmt: + gofmt -s -w . + + +###################### pkg ########################## +.ONESHELL: +gitcheck: + if [[ "$(msg)" = "" ]] ; then echo "Usage: make pkg msg='commit msg'";exit 20; fi + +.ONESHELL: +pkg: gitcheck test fmt + { hash newversion.py 2>/dev/null && newversion.py version;} ; { echo version `cat version`; } + git commit -am "$(msg)" + #jfrog "rt" "go-publish" "go-pl" $$(cat version) "--url=$$GOPROXY_API" --user=$$GOPROXY_USER --apikey=$$GOPROXY_PASS + v=`cat version` && git tag "$$v" && git push origin "$$v" && git push origin HEAD +pkg0: test + v=`cat version` && git tag "$$v" && git push origin "$$v" && git push origin HEAD +report: + goreportcard-cli -v diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..c61d471 --- /dev/null +++ b/readme.md @@ -0,0 +1,10 @@ +# 🛠️ Go http tool +[![tag](https://img.shields.io/github/tag/ahuigo/gohttptool.svg)](https://github.com/ahuigo/gohttptool/tags) +![Go Version](https://img.shields.io/badge/Go-%3E%3D%201.21-%23007d9c) +[![GoDoc](https://godoc.org/github.com/ahuigo/gohttptool?status.svg)](https://pkg.go.dev/github.com/ahuigo/gohttptool) +![Build Status](https://github.com/ahuigo/gohttptool/actions/workflows/test.yml/badge.svg) +[![Go report](https://goreportcard.com/badge/github.com/ahuigo/gohttptool)](https://goreportcard.com/report/github.com/ahuigo/gohttptool) +[![Coverage](https://img.shields.io/codecov/c/github/ahuigo/gohttptool)](https://codecov.io/gh/ahuigo/gohttptool) +[![Contributors](https://img.shields.io/github/contributors/ahuigo/gohttptool)](https://github.com/ahuigo/gohttptool/graphs/contributors) +[![License](https://img.shields.io/github/license/ahuigo/gohttptool)](./LICENSE) + diff --git a/version b/version new file mode 100644 index 0000000..95e94cd --- /dev/null +++ b/version @@ -0,0 +1 @@ +v0.0.1 \ No newline at end of file