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 2198f51
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
29 changes: 21 additions & 8 deletions spx-backend/internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"regexp"
"time"

_ "image/png"

Expand Down Expand Up @@ -497,28 +498,40 @@ func (ctrl *Controller) FmtCode(ctx context.Context, input *FmtCodeInput) (*fmtc
type UpInfo struct {
// Uptoken
Token string `json:"token"`
// Valid time for uptoken, unit: second
Expires uint64 `json:"expires"`
// Timestamp after which the uptoken is considered expired
ExpiresAt uint64 `json:"expiresAt"`
// Maximum file size allowed
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,

// There is a chance that object key collision happens, so we
// use insertOnly mode to prevent unintended overwrites.
InsertOnly: 1,

// 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)
expiresAt := uint64(time.Now().Unix()) + putPolicy.Expires
return &UpInfo{
Token: upToken,
Expires: expires,
Region: ctrl.kodo.bucketRegion,
BaseUrl: ctrl.kodo.baseUrl,
Token: upToken,
ExpiresAt: expiresAt,
MaxSize: putPolicy.FsizeLimit,
Region: ctrl.kodo.bucketRegion,
BaseUrl: ctrl.kodo.baseUrl,
}, nil
}
6 changes: 4 additions & 2 deletions spx-gui/src/apis/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ export function formatSpxCode(body: string) {
export type UpInfo = {
/** Uptoken */
token: string
/** Valid time for uptoken, unit: second */
expires: number
/** Timestamp after which the uptoken is considered expired */
expiresAt: number
/** Maximum file size allowed */
maxSize: number
/** Base URL to fetch file */
baseUrl: string
/** Bucket Region */
Expand Down
13 changes: 7 additions & 6 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 @@ -108,8 +109,8 @@ async function upload(file: File) {
return baseUrl + '/' + key
}

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

Expand All @@ -119,10 +120,10 @@ let fetchingUpInfo: Promise<UpInfo> | null = null
async function getUpInfo() {
if (upInfo != null && upInfo.expiresAt > Date.now()) return upInfo
if (fetchingUpInfo != null) return fetchingUpInfo
return (fetchingUpInfo = getRawUpInfo().then(({ expires, ...others }) => {
return (fetchingUpInfo = getRawUpInfo().then(({ expiresAt, ...others }) => {
const bufferTime = 5 * 60 * 1000 // refresh uptoken 5min before it expires
const expiresAt = Date.now() + expires * 1000 - bufferTime
upInfo = { ...others, expiresAt: expiresAt }
const localExpiresAt = expiresAt * 1000 - bufferTime
upInfo = { ...others, expiresAt: localExpiresAt }
fetchingUpInfo = null
return upInfo
}))
Expand Down

0 comments on commit 2198f51

Please sign in to comment.