Skip to content

Commit

Permalink
feat(file): generate file header
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuigo committed Jul 12, 2024
0 parents commit 444249e
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cover.out
/tmp
__debug_bin*
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
59 changes: 59 additions & 0 deletions file/file-header.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/ahuigo/gohttptool

go 1.22.1
28 changes: 28 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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)

1 change: 1 addition & 0 deletions version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.0.1

0 comments on commit 444249e

Please sign in to comment.