Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions internal/api/public/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 17 additions & 7 deletions internal/service/upload_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"log/slog"
"mime/multipart"
"path/filepath"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
33 changes: 17 additions & 16 deletions scripts/build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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 失锟杰o拷停止锟斤拷锟诫
popd
exit /b %ERRORLEVEL%
)
call npm run build
if %ERRORLEVEL% neq 0 (
echo 前端构建失败,停止编译
echo 前锟剿癸拷锟斤拷失锟杰o拷停止锟斤拷锟诫
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 锟斤拷平台锟斤拷锟斤拷锟缴o拷锟斤拷锟侥柯�: %OUTPUT_DIR%
pause
39 changes: 20 additions & 19 deletions scripts/build.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# 构建脚本
# 切换到项目根目录
# 锟斤拷锟斤拷锟脚憋拷
# 锟叫伙拷锟斤拷锟斤拷目锟斤拷目录
Set-Location -Path (Join-Path $PSScriptRoot "..")

$APP_NAME = "filerelay"
$OUTPUT_DIR = "output"

# 定义目标平台
# 锟斤拷锟斤拷目锟斤拷平台
$PLATFORMS = @(
"linux/amd64",
"linux/arm64",
Expand All @@ -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 失锟杰o拷停止锟斤拷锟诫。" -ForegroundColor Red
Pop-Location
exit $LASTEXITCODE
}
npm run build
if ($LASTEXITCODE -ne 0) {
Write-Host "前端构建失败,停止编译。" -ForegroundColor Red
Write-Host "前锟剿癸拷锟斤拷失锟杰o拷停止锟斤拷锟诫。" -ForegroundColor Red
Pop-Location
exit $LASTEXITCODE
}
Pop-Location

# 循环构建各平台
# 循锟斤拷锟斤拷锟斤拷锟斤拷平台
foreach ($PLATFORM in $PLATFORMS) {
$parts = $PLATFORM -split "/"
$os = $parts[0]
Expand All @@ -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 "锟斤拷平台锟斤拷锟斤拷锟缴o拷锟斤拷锟侥柯�: $OUTPUT_DIR" -ForegroundColor Green
2 changes: 1 addition & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ for PLATFORM in "${PLATFORMS[@]}"; do
fi

echo "姝e湪缂栬瘧 ${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} 缂栬瘧鎴愬姛"
Expand Down
28 changes: 14 additions & 14 deletions webapp/src/views/UploadPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@
<div class="text-xs text-gray-700 dark:text-gray-300 mb-2 font-semibold">{{ t('upload.settings.expireTime') }}</div>
<div class="flex flex-wrap gap-1.5">
<button
v-for="option in publicConfig.getExpireOptions().filter(opt => opt.value > 0)"
v-for="option in publicConfig.getExpireOptions()"
:key="'time-' + option.value"
@click="expireType = 'time'; expireDays = String(option.value); showCustomExpire = false"
@click="expireType = option.value === 0 ? 'permanent' : 'time'; expireDays = String(option.value); showCustomExpire = false"
:class="[
'px-2.5 py-1 text-xs font-semibold rounded-full transition-all',
expireType === 'time' && expireDays === String(option.value) && !showCustomExpire
(expireType === 'time' || expireType === 'permanent') && expireDays === String(option.value) && !showCustomExpire
? 'bg-gradient-to-r from-blue-500 to-blue-600 text-white shadow-sm'
: 'bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-600 border border-gray-200 dark:border-gray-600'
]"
Expand Down Expand Up @@ -229,12 +229,12 @@
<div class="text-xs text-gray-700 dark:text-gray-300 mb-2 font-semibold">{{ t('upload.settings.maxDownloads') }}</div>
<div class="flex flex-wrap gap-1.5">
<button
v-for="option in publicConfig.getDownloadOptions().filter(opt => opt.value > 0)"
v-for="option in publicConfig.getDownloadOptions()"
:key="'download-' + option.value"
@click="expireType = 'download'; maxDownloads = String(option.value); showCustomDownloads = false"
@click="expireType = option.value === 0 ? 'permanent' : 'download'; maxDownloads = String(option.value); showCustomDownloads = false"
:class="[
'px-2.5 py-1 text-xs font-semibold rounded-full transition-all',
expireType === 'download' && maxDownloads === String(option.value) && !showCustomDownloads
(expireType === 'download' || expireType === 'permanent') && maxDownloads === String(option.value) && !showCustomDownloads
? 'bg-gradient-to-r from-purple-500 to-purple-600 text-white shadow-sm'
: 'bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-600 border border-gray-200 dark:border-gray-600'
]"
Expand Down Expand Up @@ -631,14 +631,14 @@ const uploadFiles = async () => {
})

// 娣诲姞閰嶇疆
const config = {
const configData = {
expire_type: expireType.value,
[expireType.value === 'time' ? 'expire_days' : 'max_downloads']:
expireType.value === 'time' ? parseInt(expireDays.value) : parseInt(maxDownloads.value),
expire_days: expireType.value === 'time' ? parseInt(expireDays.value) : undefined,
max_downloads: expireType.value === 'download' ? parseInt(maxDownloads.value) : undefined,
remark: remark.value.trim() || undefined
}

Object.entries(config).forEach(([key, value]) => {
Object.entries(configData).forEach(([key, value]) => {
if (value !== undefined) {
formData.append(key, String(value))
}
Expand Down Expand Up @@ -698,15 +698,15 @@ const uploadText = async () => {
}, 100)

try {
const config = {
const configData = {
content: textContent.value.trim(),
expire_type: expireType.value,
[expireType.value === 'time' ? 'expire_days' : 'max_downloads']:
expireType.value === 'time' ? parseInt(expireDays.value) : parseInt(maxDownloads.value),
expire_days: expireType.value === 'time' ? parseInt(expireDays.value) : undefined,
max_downloads: expireType.value === 'download' ? parseInt(maxDownloads.value) : undefined,
remark: remark.value.trim() || undefined
}

const response = await publicApi.uploadText(config)
const response = await publicApi.uploadText(configData)

if (response.data.code === 200) {
uploadProgress.value = 100
Expand Down
Loading