Skip to content

Commit

Permalink
Merge pull request #11 from vvbbnn00/dev
Browse files Browse the repository at this point in the history
fix: Correct the bug where JSON is returned twice consecutively on upload failure
  • Loading branch information
vvbbnn00 authored Mar 13, 2024
2 parents eefd1d4 + 5aad0e3 commit 198e8b6
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ cert
*.syso
winres/*
.github
/dist
/dist
/goflet-data
47 changes: 47 additions & 0 deletions .github/workflows/docker_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Docker Build and Push

on:
push:
branches:
- master
workflow_dispatch:

jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: vvbbnn00/goflet
tags: |
type=sha,format=short
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: GITHUB_ACTIONS=true
platforms: linux/amd64,linux/arm64,linux/386,linux/arm/v7

- name: Image digest
run: echo ${{ steps.meta.outputs.digest }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ cert
*.exe
winres.json
*.syso
/dist
/dist
/goflet-data
11 changes: 6 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ COPY go.mod ./
RUN go mod download
COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -o goflet .
RUN CGO_ENABLED=0 GOOS=linux go build -o goflet -trimpath \
-ldflags="-s -w -X 'github.com/vvbbnn00/goflet/base.Version=$(git tag --sort=-creatordate | head -n 1) (commit:$(git rev-parse --short HEAD))'"

FROM alpine:3.19

WORKDIR /app/
COPY --from=builder /app/goflet .
VOLUME /app/data
WORKDIR /data/

COPY --from=builder /app/goflet /bin/goflet
VOLUME /data/
EXPOSE 8080

CMD ["./goflet"]
CMD ["goflet"]
3 changes: 2 additions & 1 deletion base/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func PrintBanner() {
/ /_\/ / _ \ | |_ | | / _ \| __|
/ /_\\ | (_) || _|| || __/| |_
\____/ \___/ |_| |_| \___| \__|
  Goflet version: %s
Goflet version: %s
「さぁ、始まるザマスよ!」
Expand Down
4 changes: 1 addition & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ services:
ports:
- "8080:8080"
volumes:
- ./goflet.json:/app/goflet.json
- ./data:/app/data
- ./upload:/app/upload
- ./goflet-data:/data
extra_hosts:
- "host.docker.internal:host-gateway"
restart: always
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/bytedance/sonic v1.11.2 // indirect
github.com/bytedance/sonic v1.11.3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.11.2 h1:ywfwo0a/3j9HR8wsYGWsIWl2mvRsI950HyoxiBERw5A=
github.com/bytedance/sonic v1.11.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/bytedance/sonic v1.11.3 h1:jRN+yEjakWh8aK5FzrciUHG8OFXK+4/KrAX/ysEtHAA=
github.com/bytedance/sonic v1.11.3/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
Expand Down
17 changes: 11 additions & 6 deletions route/file/operation_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func routePostFile(c *gin.Context) {
return
}
// If the file is not nil, handle the single file upload
handleSingleFileUpload(file, c)
err = handleSingleFileUpload(file, c)
if err != nil {
return // Avoid calling handleCompleteFileUpload
}

// Complete the file upload
relativePath := c.GetString("relativePath")
Expand Down Expand Up @@ -81,41 +84,43 @@ func routeDeleteFile(c *gin.Context) {
}

// handleSingleFileUpload handles the single file upload
func handleSingleFileUpload(file *multipart.FileHeader, c *gin.Context) {
func handleSingleFileUpload(file *multipart.FileHeader, c *gin.Context) error {
// Get temp file write stream
relativePath := c.GetString("relativePath")
writeStream, err := upload.GetTempFileWriteStream(relativePath)
if err != nil {
errStr := err.Error()
if errStr == "directory_creation" {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "Directory creation not allowed"})
return
return err
}
log.Warnf("Error getting write stream: %s", errStr)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Error writing file"})
return
return err
}

// Open the file
fileReader, err := file.Open()
if err != nil {
log.Warnf("Error opening file: %s", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Error reading file"})
return
return err
}

// Copy the file to the write stream
_, err = io.Copy(writeStream, fileReader)
if err != nil {
log.Warnf("Error copying file: %s", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Error writing file"})
return
return err
}

// Close the file
_ = fileReader.Close()
// Close the write stream
_ = writeStream.Close()

return nil
}

// handleCompleteFileUpload handles the completion of the file upload
Expand Down

0 comments on commit 198e8b6

Please sign in to comment.