Skip to content

Commit

Permalink
spx-backend: refactor /util/upinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
aofei committed May 11, 2024
1 parent 75f2323 commit 5e628ba
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
15 changes: 11 additions & 4 deletions spx-backend/internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,25 +499,32 @@ type UpInfo struct {
Token string `json:"token"`
// Valid time for uptoken, unit: second
Expires uint64 `json:"expires"`
// Maximum file size allowed in bytes
MaxSize int64 `json:"maxSize"`
// Bucket Region
Region string `json:"region"`
// Base URL to fetch file
BaseUrl string `json:"baseUrl"`
}

func (ctrl *Controller) GetUpInfo(ctx context.Context) (*UpInfo, error) {
var expires uint64 = 1800 // second
putPolicy := qiniuStorage.PutPolicy{
Scope: ctrl.kodo.bucket,
Expires: 1800, // 30 minutes
ForceSaveKey: true,
SaveKey: "files/$(etag)/$(fname)",
Expires: expires,
SaveKey: "files/$(etag)-$(fsize)",

// The hardcoded size limit here should be sufficient for most
// frontend use cases. If needed, we can make it configurable in
// the future.
FsizeLimit: 25 << 20, // 25 MiB
}
mac := qiniuAuth.New(ctrl.kodo.ak, ctrl.kodo.sk)
upToken := putPolicy.UploadToken(mac)
return &UpInfo{
Token: upToken,
Expires: expires,
Expires: putPolicy.Expires,
MaxSize: putPolicy.FsizeLimit,
Region: ctrl.kodo.bucketRegion,
BaseUrl: ctrl.kodo.baseUrl,
}, nil
Expand Down
2 changes: 2 additions & 0 deletions spx-gui/src/apis/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export type UpInfo = {
token: string
/** Valid time for uptoken, unit: second */
expires: number
/** Maximum file size allowed in bytes */
maxSize: number
/** Base URL to fetch file */
baseUrl: string
/** Bucket Region */
Expand Down
5 changes: 3 additions & 2 deletions spx-gui/src/models/common/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ type QiniuUploadRes = {

async function upload(file: File) {
const nativeFile = await toNativeFile(file)
const { token, baseUrl, region } = await getUpInfo()
const { token, maxSize, baseUrl, region } = await getUpInfo()
if (nativeFile.size > maxSize) throw new Error(`file size exceeds the limit (${maxSize} bytes)`)
const observable = qiniu.upload(
nativeFile,
null,
Expand All @@ -109,7 +110,7 @@ async function upload(file: File) {
}

type UpInfo = Omit<RawUpInfo, 'expires'> & {
/** Expire timestamp (ms) */
/** Timestamp (ms) after which the uptoken is considered expired */
expiresAt: number
}

Expand Down

0 comments on commit 5e628ba

Please sign in to comment.