mirror of
https://github.com/IceWhaleTech/CasaOS.git
synced 2025-12-23 13:04:42 +00:00
Compare commits
13 Commits
v0.4.4-3-a
...
v0.4.7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
53c3879907 | ||
|
|
ef57d3348d | ||
|
|
7a76aca022 | ||
|
|
fffdc7fd5e | ||
|
|
29d16d13ba | ||
|
|
54a115ae89 | ||
|
|
17fa7868a4 | ||
|
|
27d6dd86e2 | ||
|
|
ae50a9bb17 | ||
|
|
caf3347da9 | ||
|
|
718a08eab2 | ||
|
|
6f722b3506 | ||
|
|
e722d841a9 |
2
.github/workflows/codecov.yml
vendored
2
.github/workflows/codecov.yml
vendored
@@ -20,6 +20,8 @@ jobs:
|
||||
uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: "1.20"
|
||||
- name: generate OPENAPI
|
||||
run: go generate
|
||||
- name: Run coverage
|
||||
run: go test -race -failfast -coverprofile=coverage.txt -covermode=atomic -v ./...
|
||||
- name: Upload coverage to Codecov
|
||||
|
||||
6
.github/workflows/push_events_to_discord.yml
vendored
6
.github/workflows/push_events_to_discord.yml
vendored
@@ -40,9 +40,3 @@ jobs:
|
||||
uses: joseph-montanez/forward-event-action@v3.0.0
|
||||
with:
|
||||
webhook: ${{ secrets.Discord_CasaOS_Bug_Webhook }}
|
||||
|
||||
- name: Alpha Issues & Comments
|
||||
if: ${{ ( github.event_name == 'issues' || github.event_name == 'issue_comment' ) && contains(github.event.issue.labels.*.name, 'alpha') }}
|
||||
uses: joseph-montanez/forward-event-action@v3.0.0
|
||||
with:
|
||||
webhook: ${{ secrets.Discord_CasaOS_Alpha_Webhook }}
|
||||
|
||||
@@ -21,7 +21,7 @@ builds:
|
||||
- -X main.date={{.Date}}
|
||||
- -X github.com/IceWhaleTech/CasaOS/drivers/google_drive.client_id={{.Env.GoogleID}}
|
||||
- -X github.com/IceWhaleTech/CasaOS/drivers/google_drive.client_secret={{.Env.GoogleSecret}}
|
||||
- -X github.com/IceWhaleTech/CasaOS/drivers/onedrive.client_id={{.Env.OneDrivePublic}}
|
||||
- -X github.com/IceWhaleTech/CasaOS/drivers/onedrive.client_id={{.Env.OneDriveID}}
|
||||
- -X github.com/IceWhaleTech/CasaOS/drivers/onedrive.client_secret={{.Env.OneDriveSecret}}
|
||||
- -X github.com/IceWhaleTech/CasaOS/drivers/dropbox.app_key={{.Env.DropboxKey}}
|
||||
- -X github.com/IceWhaleTech/CasaOS/drivers/dropbox.app_secret={{.Env.DropboxSecret}}
|
||||
|
||||
@@ -46,6 +46,11 @@
|
||||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
||||
<a href="#credits"><img alt="All Contributors" src="https://img.shields.io/static/v1?label=All%20Contributors&message=15&color=162453&style=flat-square&logo=Handshake&logoColor=fff" /></a>
|
||||
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
||||
<br/>
|
||||
<!-- CasaOS YouTube -->
|
||||
<a href="https://www.youtube.com/channel/UC2zMrUYT17AJhIl9XWZzT8g" target="_blank">
|
||||
<img alt="YouTube Tutoial Views" src="https://img.shields.io/youtube/channel/views/UC2zMrUYT17AJhIl9XWZzT8g?style=for-the-badge&logo=youtube&logoColor=red&label=YouTube%20Tutoial%20Views" />
|
||||
</a>
|
||||
<br/>
|
||||
<!-- CasaOS Links -->
|
||||
<a href="https://www.casaos.io" target="_blank">Website</a> |
|
||||
|
||||
@@ -90,6 +90,107 @@ paths:
|
||||
$ref: "#/components/responses/ResponseOK"
|
||||
"500":
|
||||
$ref: "#/components/responses/ResponseInternalServerError"
|
||||
|
||||
/file/upload:
|
||||
get:
|
||||
tags:
|
||||
- File
|
||||
summary: Check upload chunk
|
||||
parameters:
|
||||
- name: path
|
||||
in: query
|
||||
description: File path
|
||||
required: true
|
||||
example: "/DATA/test.log"
|
||||
schema:
|
||||
type: string
|
||||
- name: relativePath
|
||||
in: query
|
||||
description: File path
|
||||
required: true
|
||||
example: "/DATA/test.log"
|
||||
schema:
|
||||
type: string
|
||||
- name: filename
|
||||
in: query
|
||||
description: File name
|
||||
required: true
|
||||
example: "test.log"
|
||||
schema:
|
||||
type: string
|
||||
- name: chunkNumber
|
||||
in: query
|
||||
description: chunk number
|
||||
required: true
|
||||
example: 1
|
||||
schema:
|
||||
type: string
|
||||
- name: totalChunks
|
||||
in: query
|
||||
description: total chunks
|
||||
example: 2
|
||||
required: true
|
||||
schema:
|
||||
type: integer
|
||||
description: Check if the file block has been uploaded (needs to be modified later)
|
||||
operationId: checkUploadChunk
|
||||
responses:
|
||||
"200":
|
||||
$ref: "#/components/responses/ResponseStringOK"
|
||||
"400":
|
||||
$ref: "#/components/responses/ResponseClientError"
|
||||
"500":
|
||||
$ref: "#/components/responses/ResponseInternalServerError"
|
||||
post:
|
||||
tags:
|
||||
- File
|
||||
summary: Upload file
|
||||
description: Upload file
|
||||
operationId: postUploadFile
|
||||
requestBody:
|
||||
content:
|
||||
multipart/form-data:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
relativePath:
|
||||
type: string
|
||||
example: "/DATA/test.log"
|
||||
filename:
|
||||
type: string
|
||||
example: "/DATA/test2.log"
|
||||
totalChunks:
|
||||
type: string
|
||||
example: "2"
|
||||
chunkNumber:
|
||||
type: string
|
||||
example: "20"
|
||||
path:
|
||||
type: string
|
||||
example: "/DATA"
|
||||
file:
|
||||
type: string
|
||||
format: binary
|
||||
chunkSize:
|
||||
type: string
|
||||
example: "1024"
|
||||
currentChunkSize:
|
||||
type: string
|
||||
example: "1024"
|
||||
totalSize:
|
||||
type: string
|
||||
example: "1024"
|
||||
identifier:
|
||||
type: string
|
||||
example: "test.log"
|
||||
responses:
|
||||
"200":
|
||||
$ref: "#/components/responses/ResponseStringOK"
|
||||
"400":
|
||||
$ref: "#/components/responses/ResponseClientError"
|
||||
"500":
|
||||
$ref: "#/components/responses/ResponseInternalServerError"
|
||||
|
||||
/zt/info:
|
||||
get:
|
||||
tags:
|
||||
@@ -151,6 +252,20 @@ components:
|
||||
schema:
|
||||
$ref: "#/components/schemas/BaseResponse"
|
||||
|
||||
ResponseStringOK:
|
||||
description: OK
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/SuccessResponseString"
|
||||
|
||||
ResponseClientError:
|
||||
description: Client Error
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/BaseResponse"
|
||||
|
||||
ResponseInternalServerError:
|
||||
description: Internal Server Error
|
||||
content:
|
||||
@@ -196,6 +311,14 @@ components:
|
||||
type: string
|
||||
example: ""
|
||||
|
||||
SuccessResponseString:
|
||||
allOf:
|
||||
- $ref: "#/components/schemas/BaseResponse"
|
||||
- properties:
|
||||
data:
|
||||
type: string
|
||||
description: When the interface returns success, this field is the specific success information
|
||||
|
||||
HealthServices:
|
||||
properties:
|
||||
running:
|
||||
|
||||
@@ -2,7 +2,7 @@ package common
|
||||
|
||||
const (
|
||||
SERVICENAME = "casaos"
|
||||
VERSION = "0.4.4.3"
|
||||
VERSION = "0.4.7"
|
||||
BODY = " "
|
||||
RANW_NAME = "IceWhale-RemoteAccess"
|
||||
)
|
||||
|
||||
11
go.mod
11
go.mod
@@ -4,12 +4,12 @@ go 1.20
|
||||
|
||||
require (
|
||||
github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d
|
||||
github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha2
|
||||
github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha3
|
||||
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
|
||||
github.com/deckarep/golang-set/v2 v2.3.0
|
||||
github.com/deepmap/oapi-codegen v1.12.4
|
||||
github.com/disintegration/imaging v1.6.2
|
||||
github.com/dsoprea/go-exif/v3 v3.0.0-20221012082141-d21ac8e2de85
|
||||
github.com/dsoprea/go-exif/v3 v3.0.1
|
||||
github.com/getkin/kin-openapi v0.117.0
|
||||
github.com/gin-contrib/gzip v0.0.6
|
||||
github.com/gin-gonic/gin v1.9.1
|
||||
@@ -19,6 +19,7 @@ require (
|
||||
github.com/golang/mock v1.6.0
|
||||
github.com/gomodule/redigo v1.8.9
|
||||
github.com/google/go-github/v36 v36.0.0
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/googollee/go-socket.io v1.7.0
|
||||
github.com/gorilla/websocket v1.5.0
|
||||
github.com/h2non/filetype v1.1.3
|
||||
@@ -34,7 +35,6 @@ require (
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/samber/lo v1.38.1
|
||||
github.com/satori/go.uuid v1.2.0
|
||||
github.com/shirou/gopsutil/v3 v3.23.2
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
github.com/stretchr/testify v1.8.4
|
||||
@@ -44,7 +44,7 @@ require (
|
||||
golang.org/x/crypto v0.14.0
|
||||
golang.org/x/oauth2 v0.7.0
|
||||
golang.org/x/sync v0.3.0
|
||||
golang.org/x/sys v0.13.0
|
||||
golang.org/x/sys v0.14.0
|
||||
gorm.io/gorm v1.25.0
|
||||
gotest.tools v2.2.0+incompatible
|
||||
)
|
||||
@@ -82,7 +82,6 @@ require (
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/google/go-cmp v0.6.0 // indirect
|
||||
github.com/google/go-querystring v1.1.0 // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/gorilla/mux v1.8.0 // indirect
|
||||
github.com/invopop/yaml v0.2.0 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
@@ -97,7 +96,7 @@ require (
|
||||
github.com/lufia/plan9stats v0.0.0-20230110061619-bbe2e5e100de // indirect
|
||||
github.com/mailru/easyjson v0.7.7 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
|
||||
|
||||
18
go.sum
18
go.sum
@@ -1,7 +1,7 @@
|
||||
github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d h1:62lEBImTxZ83pgzywgDNIrPPuQ+j4ep9QjqrWBn1hrU=
|
||||
github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d/go.mod h1:lW9x+yEjqKdPbE3+cf2fGPJXCw/hChX3Omi9QHTLFsQ=
|
||||
github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha2 h1:8PuukQvQIlkw9mkK4THKTbSDMio9I6kbXulDTBxG2d0=
|
||||
github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha2/go.mod h1:2IuYyy5qW1BE6jqC6M+tOU+WtUec1K565rLATBJ9p/0=
|
||||
github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha3 h1:5E5LAqi2uXpOZqcPOgQ4m6d9MagYyfhKIFXnzd8s3W4=
|
||||
github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha3/go.mod h1:2IuYyy5qW1BE6jqC6M+tOU+WtUec1K565rLATBJ9p/0=
|
||||
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
|
||||
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
|
||||
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
|
||||
@@ -39,8 +39,8 @@ github.com/dsoprea/go-exif/v3 v3.0.0-20200717053412-08f1b6708903/go.mod h1:0nsO1
|
||||
github.com/dsoprea/go-exif/v3 v3.0.0-20210625224831-a6301f85c82b/go.mod h1:cg5SNYKHMmzxsr9X6ZeLh/nfBRHHp5PngtEPcujONtk=
|
||||
github.com/dsoprea/go-exif/v3 v3.0.0-20221003160559-cf5cd88aa559/go.mod h1:rW6DMEv25U9zCtE5ukC7ttBRllXj7g7TAHl7tQrT5No=
|
||||
github.com/dsoprea/go-exif/v3 v3.0.0-20221003171958-de6cb6e380a8/go.mod h1:akyZEJZ/k5bmbC9gA612ZLQkcED8enS9vuTiuAkENr0=
|
||||
github.com/dsoprea/go-exif/v3 v3.0.0-20221012082141-d21ac8e2de85 h1:vZkWA0Lduu9QB6qRBtsLy7m5uO/RFNCDH2pzMLTxGts=
|
||||
github.com/dsoprea/go-exif/v3 v3.0.0-20221012082141-d21ac8e2de85/go.mod h1:10HkA1Wz3h398cDP66L+Is9kKDmlqlIJGPv8pk4EWvc=
|
||||
github.com/dsoprea/go-exif/v3 v3.0.1 h1:/IE4iW7gvY7BablV1XY0unqhMv26EYpOquVMwoBo/wc=
|
||||
github.com/dsoprea/go-exif/v3 v3.0.1/go.mod h1:10HkA1Wz3h398cDP66L+Is9kKDmlqlIJGPv8pk4EWvc=
|
||||
github.com/dsoprea/go-logging v0.0.0-20190624164917-c4f10aab7696/go.mod h1:Nm/x2ZUNRW6Fe5C3LxdY1PyZY5wmDv/s5dkPJ/VB3iA=
|
||||
github.com/dsoprea/go-logging v0.0.0-20200517223158-a10564966e9d/go.mod h1:7I+3Pe2o/YSU88W0hWlm9S22W7XI1JFNJ86U0zPKMf8=
|
||||
github.com/dsoprea/go-logging v0.0.0-20200710184922-b02d349568dd h1:l+vLbuxptsC6VQyQsfD7NnEC8BZuFpz45PgY+pH8YTg=
|
||||
@@ -210,8 +210,8 @@ github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxec
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mholt/archiver/v3 v3.5.1 h1:rDjOBX9JSF5BvoJGvjqK479aL70qh9DIpZCl+k7Clwo=
|
||||
github.com/mholt/archiver/v3 v3.5.1/go.mod h1:e3dqJ7H78uzsRSEACH1joayhuSyhnonssnDhppzS1L4=
|
||||
github.com/mileusna/useragent v1.2.1 h1:p3RJWhi3LfuI6BHdddojREyK3p6qX67vIfOVMnUIVr0=
|
||||
@@ -260,8 +260,6 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
|
||||
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
|
||||
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
|
||||
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
|
||||
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
|
||||
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
||||
github.com/shirou/gopsutil/v3 v3.23.2 h1:PAWSuiAszn7IhPMBtXsbSCafej7PqUOvY6YywlQUExU=
|
||||
github.com/shirou/gopsutil/v3 v3.23.2/go.mod h1:gv0aQw33GLo3pG8SiWKiQrbDzbRY1K80RyZJ7V4Th1M=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
@@ -389,8 +387,8 @@ golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
|
||||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
|
||||
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
|
||||
@@ -29,7 +29,7 @@ import (
|
||||
model2 "github.com/IceWhaleTech/CasaOS/service/model"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"github.com/google/uuid"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/h2non/filetype"
|
||||
@@ -715,7 +715,7 @@ func PostOperateFileOrDir(c *gin.Context) {
|
||||
list.TotalSize = total
|
||||
list.ProcessedSize = 0
|
||||
|
||||
uid := uuid.NewV4().String()
|
||||
uid := uuid.NewString()
|
||||
service.FileQueue.Store(uid, list)
|
||||
service.OpStrArr = append(service.OpStrArr, uid)
|
||||
if len(service.OpStrArr) == 1 {
|
||||
@@ -923,7 +923,7 @@ func ConnectWebSocket(c *gin.Context) {
|
||||
peerId := c.Query("peer")
|
||||
writer := c.Writer
|
||||
request := c.Request
|
||||
key := uuid.NewV4().String()
|
||||
key := uuid.NewString()
|
||||
//peerModel := service.MyService.Peer().GetPeerByUserAgent(c.Request.UserAgent())
|
||||
peerModel := model2.PeerDriveDBModel{}
|
||||
name := service.GetName(request)
|
||||
|
||||
@@ -116,6 +116,12 @@ func InitV2Router() http.Handler {
|
||||
// })
|
||||
|
||||
e.Use(middleware.OapiRequestValidatorWithOptions(_swagger, &middleware.Options{
|
||||
Skipper: func(c echo.Context) bool {
|
||||
// jump validate when upload file
|
||||
// because file upload can't pass validate
|
||||
// issue: https://github.com/deepmap/oapi-codegen/issues/514
|
||||
return strings.Contains(c.Request().Header[echo.HeaderContentType][0], "multipart/form-data")
|
||||
},
|
||||
Options: openapi3filter.Options{AuthenticationFunc: openapi3filter.NoopAuthenticationFunc},
|
||||
}))
|
||||
|
||||
|
||||
@@ -2,7 +2,9 @@ package v2
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/IceWhaleTech/CasaOS/codegen"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
@@ -15,3 +17,70 @@ func (s *CasaOS) GetFileTest(ctx echo.Context) error {
|
||||
|
||||
return ctx.String(200, "pong")
|
||||
}
|
||||
|
||||
func (c *CasaOS) CheckUploadChunk(ctx echo.Context, params codegen.CheckUploadChunkParams) error {
|
||||
identifier := ctx.QueryParam("identifier")
|
||||
chunkNumber, err := strconv.ParseInt(ctx.QueryParam("chunkNumber"), 10, 64)
|
||||
if err != nil {
|
||||
return ctx.NoContent(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
err = c.fileUploadService.TestChunk(ctx, identifier, chunkNumber)
|
||||
if err != nil {
|
||||
return ctx.NoContent(http.StatusNoContent)
|
||||
}
|
||||
return ctx.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func (c *CasaOS) PostUploadFile(ctx echo.Context) error {
|
||||
path := ctx.FormValue("path")
|
||||
|
||||
// handle the request
|
||||
chunkNumber, err := strconv.ParseInt(ctx.FormValue("chunkNumber"), 10, 64)
|
||||
if err != nil {
|
||||
return ctx.JSON(http.StatusBadRequest, err)
|
||||
}
|
||||
chunkSize, err := strconv.ParseInt(ctx.FormValue("chunkSize"), 10, 64)
|
||||
if err != nil {
|
||||
return ctx.JSON(http.StatusBadRequest, err)
|
||||
}
|
||||
currentChunkSize, err := strconv.ParseInt(ctx.FormValue("currentChunkSize"), 10, 64)
|
||||
if err != nil {
|
||||
return ctx.JSON(http.StatusBadRequest, err)
|
||||
}
|
||||
totalChunks, err := strconv.ParseInt(ctx.FormValue("totalChunks"), 10, 64)
|
||||
if err != nil {
|
||||
return ctx.JSON(http.StatusBadRequest, err)
|
||||
}
|
||||
totalSize, err := strconv.ParseInt(ctx.FormValue("totalSize"), 10, 64)
|
||||
if err != nil {
|
||||
return ctx.JSON(http.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
identifier := ctx.FormValue("identifier")
|
||||
fileName := ctx.FormValue("filename")
|
||||
relativePath := ctx.FormValue("relativePath")
|
||||
bin, err := ctx.FormFile("file")
|
||||
|
||||
if err != nil {
|
||||
return ctx.JSON(http.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
err = c.fileUploadService.UploadFile(
|
||||
ctx,
|
||||
path,
|
||||
chunkNumber,
|
||||
chunkSize,
|
||||
currentChunkSize,
|
||||
totalChunks,
|
||||
totalSize,
|
||||
identifier,
|
||||
relativePath,
|
||||
fileName,
|
||||
bin,
|
||||
)
|
||||
if err != nil {
|
||||
return ctx.JSON(http.StatusInternalServerError, err)
|
||||
}
|
||||
return ctx.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
@@ -2,10 +2,15 @@ package v2
|
||||
|
||||
import (
|
||||
"github.com/IceWhaleTech/CasaOS/codegen"
|
||||
"github.com/IceWhaleTech/CasaOS/service"
|
||||
)
|
||||
|
||||
type CasaOS struct{}
|
||||
type CasaOS struct {
|
||||
fileUploadService *service.FileUploadService
|
||||
}
|
||||
|
||||
func NewCasaOS() codegen.ServerInterface {
|
||||
return &CasaOS{}
|
||||
return &CasaOS{
|
||||
fileUploadService: service.NewFileUploadService(),
|
||||
}
|
||||
}
|
||||
|
||||
167
service/file_upload.go
Normal file
167
service/file_upload.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
|
||||
"github.com/labstack/echo/v4"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type FileInfo struct {
|
||||
init bool
|
||||
uploaded []bool
|
||||
uploadedChunkNum int64
|
||||
}
|
||||
|
||||
type FileUploadService struct {
|
||||
uploadStatus sync.Map
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewFileUploadService() *FileUploadService {
|
||||
return &FileUploadService{
|
||||
uploadStatus: sync.Map{},
|
||||
lock: sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *FileUploadService) TestChunk(
|
||||
c echo.Context,
|
||||
identifier string,
|
||||
chunkNumber int64,
|
||||
) error {
|
||||
fileInfoTemp, ok := s.uploadStatus.Load(identifier)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("file not found")
|
||||
}
|
||||
|
||||
fileInfo := fileInfoTemp.(*FileInfo)
|
||||
|
||||
if !fileInfo.init {
|
||||
return fmt.Errorf("file not init")
|
||||
}
|
||||
|
||||
// return StatusNoContent instead of 404
|
||||
// the is require by frontend
|
||||
if !fileInfo.uploaded[chunkNumber-1] {
|
||||
return fmt.Errorf("file not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *FileUploadService) UploadFile(
|
||||
c echo.Context,
|
||||
path string,
|
||||
chunkNumber int64,
|
||||
chunkSize int64,
|
||||
currentChunkSize int64,
|
||||
totalChunks int64,
|
||||
totalSize int64,
|
||||
identifier string,
|
||||
relativePath string,
|
||||
fileName string,
|
||||
bin *multipart.FileHeader,
|
||||
) error {
|
||||
s.lock.Lock()
|
||||
fileInfoTemp, ok := s.uploadStatus.Load(identifier)
|
||||
var fileInfo *FileInfo
|
||||
|
||||
if relativePath != fileName {
|
||||
// uploaded file is folder
|
||||
folderPath := filepath.Dir(path + "/" + relativePath)
|
||||
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(folderPath, os.ModePerm)
|
||||
if err != nil {
|
||||
s.lock.Unlock()
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(path+"/"+relativePath+".tmp", os.O_WRONLY|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
s.lock.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
if !ok {
|
||||
|
||||
if err != nil {
|
||||
s.lock.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
// pre allocate file size
|
||||
fmt.Println("truncate", totalSize)
|
||||
if err != nil {
|
||||
s.lock.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
// file info init
|
||||
fileInfo = &FileInfo{
|
||||
init: true,
|
||||
uploaded: make([]bool, totalChunks),
|
||||
uploadedChunkNum: 0,
|
||||
}
|
||||
s.uploadStatus.Store(identifier, fileInfo)
|
||||
} else {
|
||||
fileInfo = fileInfoTemp.(*FileInfo)
|
||||
}
|
||||
|
||||
s.lock.Unlock()
|
||||
|
||||
_, err = file.Seek((chunkNumber-1)*chunkSize, io.SeekStart)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
src, err := bin.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
_, err = io.Copy(file, src)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
s.lock.Lock()
|
||||
// handle file after write a chunk
|
||||
// handle single chunk upload twice
|
||||
if !fileInfo.uploaded[chunkNumber-1] {
|
||||
fileInfo.uploadedChunkNum++
|
||||
fileInfo.uploaded[chunkNumber-1] = true
|
||||
}
|
||||
|
||||
// handle file after write all chunk
|
||||
if fileInfo.uploadedChunkNum == totalChunks {
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
s.lock.Unlock()
|
||||
logger.Error("close file error: ", zap.Error(err))
|
||||
}
|
||||
|
||||
err = os.Rename(path+"/"+relativePath+".tmp", path+"/"+relativePath)
|
||||
if err != nil {
|
||||
s.lock.Unlock()
|
||||
logger.Error("rename file error: ", zap.Error(err))
|
||||
}
|
||||
// remove upload status info after upload complete
|
||||
s.uploadStatus.Delete(identifier)
|
||||
}
|
||||
s.lock.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user