From ad7648828ca53f5e6483e37c6a87a35ac82b39f5 Mon Sep 17 00:00:00 2001 From: hanxuanyu <2252193204@qq.com> Date: Thu, 5 Mar 2026 16:08:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=B0=B8=E4=B9=85=E8=BF=87?= =?UTF-8?q?=E6=9C=9F=E6=97=B6=E9=97=B4=E7=B1=BB=E5=9E=8B=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E5=AD=98=E5=82=A8=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yml | 4 +-- internal/api/public/upload.go | 4 +++ internal/service/upload_service.go | 24 ++++++++++++------ scripts/build.bat | 33 +++++++++++++------------ scripts/build.ps1 | 39 +++++++++++++++--------------- scripts/build.sh | 2 +- webapp/src/views/UploadPage.vue | 28 ++++++++++----------- 7 files changed, 75 insertions(+), 59 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8ddc1cd..6be74ca 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,7 +9,7 @@ on: jobs: build: name: Build Check - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: - name: Checkout code uses: actions/checkout@v4 @@ -34,7 +34,7 @@ jobs: npm run build - name: Build Go - run: go build -v . + run: CGO_ENABLED=0 go build -v -ldflags="-s -w -extldflags=-static" . - name: Notification if: success() env: diff --git a/internal/api/public/upload.go b/internal/api/public/upload.go index 4599a15..11c778d 100644 --- a/internal/api/public/upload.go +++ b/internal/api/public/upload.go @@ -92,6 +92,8 @@ func (h *UploadHandler) Upload(c *gin.Context) { max = 1 } expireValue = max + case "permanent": + expireValue = 0 } batch, err := h.uploadService.CreateBatch(c.Request.Context(), files, remark, expireType, expireValue) @@ -158,6 +160,8 @@ func (h *UploadHandler) UploadText(c *gin.Context) { req.MaxDownloads = 1 } expireValue = req.MaxDownloads + case "permanent": + expireValue = 0 } batch, err := h.uploadService.CreateTextBatch(c.Request.Context(), req.Content, req.Remark, req.ExpireType, expireValue) diff --git a/internal/service/upload_service.go b/internal/service/upload_service.go index 4a50b21..1392125 100644 --- a/internal/service/upload_service.go +++ b/internal/service/upload_service.go @@ -9,7 +9,6 @@ import ( "fmt" "log/slog" "mime/multipart" - "path/filepath" "time" "github.com/google/uuid" @@ -51,7 +50,7 @@ func (s *UploadService) CreateBatch(ctx context.Context, files []*multipart.File } for _, fileHeader := range files { - fileItem, err := s.processFile(ctx, tx, batch.ID, fileHeader) + fileItem, err := s.processFile(ctx, tx, batch, fileHeader) if err != nil { return err } @@ -111,20 +110,31 @@ func (s *UploadService) applyExpire(batch *model.FileBatch, expireType string, e if max, ok := expireValue.(int); ok { batch.MaxDownloads = max } + case "permanent": + batch.ExpireAt = nil + batch.MaxDownloads = 0 } } -func (s *UploadService) processFile(ctx context.Context, tx *gorm.DB, batchID string, fileHeader *multipart.FileHeader) (*model.FileItem, error) { +func (s *UploadService) processFile(ctx context.Context, tx *gorm.DB, batch *model.FileBatch, fileHeader *multipart.FileHeader) (*model.FileItem, error) { file, err := fileHeader.Open() if err != nil { return nil, err } defer file.Close() - // 生成唯一存储路径 - ext := filepath.Ext(fileHeader.Filename) + // 生成用户友好的存储路径: 日期/原始文件名 + // 注意:如果同一日期下有同名文件,为了防止覆盖,需要处理冲突。 + dateStr := time.Now().Format("2006-01-02") fileID := uuid.New().String() - storagePath := fmt.Sprintf("%s/%s%s", batchID, fileID, ext) + storagePath := fmt.Sprintf("%s/%s", dateStr, fileHeader.Filename) + + // 检查存储路径是否已存在 + exists, err := storage.GlobalStorage.Exists(ctx, storagePath) + if err == nil && exists { + // 如果存在,则在文件名后附加 UUID 前缀 + storagePath = fmt.Sprintf("%s/%s_%s", dateStr, fileID[:8], fileHeader.Filename) + } // 保存到存储层 if err := storage.GlobalStorage.Save(ctx, storagePath, file); err != nil { @@ -134,7 +144,7 @@ func (s *UploadService) processFile(ctx context.Context, tx *gorm.DB, batchID st // 创建数据库记录 item := &model.FileItem{ ID: fileID, - BatchID: batchID, + BatchID: batch.ID, OriginalName: fileHeader.Filename, StoragePath: storagePath, Size: fileHeader.Size, diff --git a/scripts/build.bat b/scripts/build.bat index cadcce8..64d017c 100644 --- a/scripts/build.bat +++ b/scripts/build.bat @@ -2,66 +2,67 @@ chcp 65001 > nul setlocal enabledelayedexpansion -:: лĿĿ¼ +:: �л�����Ŀ��Ŀ¼ cd /d "%~dp0.." set APP_NAME=filerelay set OUTPUT_DIR=output -echo ʼ %APP_NAME% ƽ̨ļ... +echo ��ʼ���� %APP_NAME% ��ƽ̨�������ļ�... -:: output Ŀ¼ +:: ���� output Ŀ¼ if exist "%OUTPUT_DIR%" ( - echo %OUTPUT_DIR% Ŀ¼... + echo �������� %OUTPUT_DIR% Ŀ¼... rd /s /q "%OUTPUT_DIR%" ) mkdir "%OUTPUT_DIR%" -:: ǰ˹ -echo ڹǰĿ... +:: ǰ�˹��� +echo ���ڹ���ǰ����Ŀ... pushd webapp call npm install if %ERRORLEVEL% neq 0 ( - echo npm install ʧܣֹͣ롣 + echo npm install ʧ�ܣ�ֹͣ���롣 popd exit /b %ERRORLEVEL% ) call npm run build if %ERRORLEVEL% neq 0 ( - echo ǰ˹ʧܣֹͣ롣 + echo ǰ�˹���ʧ�ܣ�ֹͣ���롣 popd exit /b %ERRORLEVEL% ) popd -:: Ŀƽ̨ (OS/Arch) +:: ����Ŀ��ƽ̨ (OS/Arch) set PLATFORMS=linux/amd64 linux/arm64 windows/amd64 windows/arm64 darwin/amd64 darwin/arm64 for %%P in (%PLATFORMS%) do ( for /f "tokens=1,2 delims=/" %%A in ("%%P") do ( + set CGO_ENABLED=0 set GOOS=%%A set GOARCH=%%B set OUTPUT_NAME=%APP_NAME%-%%A-%%B if "%%A"=="windows" set OUTPUT_NAME=!OUTPUT_NAME!.exe - echo ڱ %%A/%%B... + echo ���ڱ��� %%A/%%B... - go build -o "%OUTPUT_DIR%\!OUTPUT_NAME!" main.go + go build -ldflags="-s -w -extldflags=-static" -o "%OUTPUT_DIR%\!OUTPUT_NAME!" main.go if !ERRORLEVEL! equ 0 ( - echo %%A/%%B ɹ - :: ѹΪ tar.gz (Windows 10+ Դ tar) + echo %%A/%%B ����ɹ� + :: ѹ��Ϊ tar.gz (Windows 10+ �Դ� tar) tar -czf "%OUTPUT_DIR%\!OUTPUT_NAME!.tar.gz" -C "%OUTPUT_DIR%" "!OUTPUT_NAME!" - :: ɾԭʼļ + :: ɾ��ԭʼ�������ļ� del "%OUTPUT_DIR%\!OUTPUT_NAME!" ) else ( - echo %%A/%%B ʧ + echo %%A/%%B ����ʧ�� ) ) ) echo ---------------------------------------- -echo ƽ̨ɣĿ¼: %OUTPUT_DIR% +echo ��ƽ̨�����ɣ����Ŀ¼: %OUTPUT_DIR% pause diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 5c33460..0d37f8e 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -1,11 +1,11 @@ -# ű -# лĿĿ¼ +# �����ű� +# �л�����Ŀ��Ŀ¼ Set-Location -Path (Join-Path $PSScriptRoot "..") $APP_NAME = "filerelay" $OUTPUT_DIR = "output" -# Ŀƽ̨ +# ����Ŀ��ƽ̨ $PLATFORMS = @( "linux/amd64", "linux/arm64", @@ -15,34 +15,34 @@ $PLATFORMS = @( "darwin/arm64" ) -Write-Host "ʼ $APP_NAME ƽ̨ļ..." -ForegroundColor Cyan +Write-Host "��ʼ���� $APP_NAME ��ƽ̨�������ļ�..." -ForegroundColor Cyan -# output Ŀ¼ +# ���� output Ŀ¼ if (Test-Path $OUTPUT_DIR) { - Write-Host " $OUTPUT_DIR Ŀ¼..." + Write-Host "�������� $OUTPUT_DIR Ŀ¼..." Remove-Item -Path $OUTPUT_DIR -Recurse -Force } New-Item -Path $OUTPUT_DIR -ItemType Directory -Force | Out-Null -# ǰ˹ -Write-Host "ڹǰĿ..." -ForegroundColor Cyan +# ǰ�˹��� +Write-Host "���ڹ���ǰ����Ŀ..." -ForegroundColor Cyan Push-Location webapp npm install if ($LASTEXITCODE -ne 0) { - Write-Host "npm install ʧܣֹͣ롣" -ForegroundColor Red + Write-Host "npm install ʧ�ܣ�ֹͣ���롣" -ForegroundColor Red Pop-Location exit $LASTEXITCODE } npm run build if ($LASTEXITCODE -ne 0) { - Write-Host "ǰ˹ʧܣֹͣ롣" -ForegroundColor Red + Write-Host "ǰ�˹���ʧ�ܣ�ֹͣ���롣" -ForegroundColor Red Pop-Location exit $LASTEXITCODE } Pop-Location -# ѭƽ̨ +# ѭ��������ƽ̨ foreach ($PLATFORM in $PLATFORMS) { $parts = $PLATFORM -split "/" $os = $parts[0] @@ -53,26 +53,27 @@ foreach ($PLATFORM in $PLATFORMS) { $outputName += ".exe" } - Write-Host "ڱ $($os)/$($arch)..." + Write-Host "���ڱ��� $($os)/$($arch)..." + $env:CGO_ENABLED = "0" $env:GOOS = $os $env:GOARCH = $arch - go build -o (Join-Path $OUTPUT_DIR $outputName) main.go + go build -ldflags="-s -w -extldflags=-static" -o (Join-Path $OUTPUT_DIR $outputName) main.go if ($LASTEXITCODE -eq 0) { - Write-Host " $($os)/$($arch) ɹ" -ForegroundColor Green - # ѹΪ tar.gz + Write-Host " $($os)/$($arch) ����ɹ�" -ForegroundColor Green + # ѹ��Ϊ tar.gz tar -czf (Join-Path $OUTPUT_DIR "$outputName.tar.gz") -C $OUTPUT_DIR $outputName - # ɾԭʼļ + # ɾ��ԭʼ�������ļ� Remove-Item (Join-Path $OUTPUT_DIR $outputName) } else { - Write-Host " $($os)/$($arch) ʧ" -ForegroundColor Red + Write-Host " $($os)/$($arch) ����ʧ��" -ForegroundColor Red } } -# û +# ���û������� $env:GOOS = $null $env:GOARCH = $null Write-Host "----------------------------------------" -ForegroundColor Cyan -Write-Host "ƽ̨ɣĿ¼: $OUTPUT_DIR" -ForegroundColor Green +Write-Host "��ƽ̨�����ɣ����Ŀ¼: $OUTPUT_DIR" -ForegroundColor Green diff --git a/scripts/build.sh b/scripts/build.sh index 1b7f37e..af93a8e 100644 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -51,7 +51,7 @@ for PLATFORM in "${PLATFORMS[@]}"; do fi echo "正在编译 ${OS}/${ARCH}..." - GOOS=$OS GOARCH=$ARCH go build -o "${OUTPUT_DIR}/${OUTPUT_NAME}" main.go + CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -ldflags="-s -w -extldflags=-static" -o "${OUTPUT_DIR}/${OUTPUT_NAME}" main.go if [ $? -eq 0 ]; then echo " ${OS}/${ARCH} 编译成功" diff --git a/webapp/src/views/UploadPage.vue b/webapp/src/views/UploadPage.vue index b5f5188..f9637bc 100644 --- a/webapp/src/views/UploadPage.vue +++ b/webapp/src/views/UploadPage.vue @@ -184,12 +184,12 @@
{{ t('upload.settings.expireTime') }}