Skip to content

Commit

Permalink
sv
Browse files Browse the repository at this point in the history
  • Loading branch information
nighca committed Mar 21, 2024
1 parent 4a661aa commit bd972b5
Show file tree
Hide file tree
Showing 13 changed files with 582 additions and 366 deletions.
22 changes: 11 additions & 11 deletions spx-gui/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/// <reference types="vite/client" />

interface ImportMetaEnv {
readonly VITE_API_BASE_URL: string
readonly VITE_PUBLISH_BASE_URL:string
readonly VITE_CASDOOR_ENDPOINT: string
readonly VITE_CASDOOR_CLIENT_ID: string
readonly VITE_CASDOOR_ORGANIZATION_NAME: string
readonly VITE_CASDOOR_APP_NAME: string
}
// interface ImportMetaEnv {
// readonly VITE_API_BASE_URL: string
// readonly VITE_PUBLISH_BASE_URL:string
// readonly VITE_CASDOOR_ENDPOINT: string
// readonly VITE_CASDOOR_CLIENT_ID: string
// readonly VITE_CASDOOR_ORGANIZATION_NAME: string
// readonly VITE_CASDOOR_APP_NAME: string
// }

interface ImportMeta {
readonly env: ImportMetaEnv
}
// interface ImportMeta {
// readonly env: ImportMetaEnv
// }
276 changes: 53 additions & 223 deletions spx-gui/src/api/asset.ts
Original file line number Diff line number Diff line change
@@ -1,244 +1,74 @@
/*
* @Author: Yao xinyue
* @Date: 2024-01-22 11:17:08
* @LastEditors: xuning [email protected]
* @LastEditTime: 2024-03-14 11:22:33
* @FilePath: \spx-gui\src\api\asset.ts
* @Description:
*/
import { service } from '@/axios'
import type { Asset, PageAssetResponse } from '@/interface/library.ts' // Adjust the import paths as needed
import type { ResponseData } from '@/axios'
import type { AxiosResponse } from 'axios'
import { PublicStatus } from '@/class/project'
import type { ByPage, PaginationParams, IsPublic, FileCollection } from './common'
import { client } from './common'

export enum PublishState {
NotPublished = -1,
PrivateLibrary = 0,
PublicAndPrivateLibrary = 1
}

/**
* Fetches a list of assets
*
* @param assetLibraryType 'public' / 'private';
* @param pageIndex The index of the page to retrieve in a paginated list.
* @param pageSize The number of assets to retrieve per page.
* @param assetType The type of the asset. See src/constant/constant.ts for details.
* @param category (Optional) The category of the assets to filter by.
* @param isOrderByTime (Optional) Whether to order assets by time.
* @param isOrderByHot (Optional) Whether to order assets by popularity.
* @returns PageAssetResponse
*/
export function getAssetList({
isPublic,
pageIndex,
pageSize,
assetType,
category,
isOrderByTime,
isOrderByHot,
author
}: {
pageIndex: number
pageSize: number
assetType: number
category?: string
isOrderByTime?: boolean
isOrderByHot?: boolean
isPublic?: PublicStatus
author?: string
}): Promise<PageAssetResponse> {
const baseAssetUrl = '/assets/list'
const params = new URLSearchParams()
params.append('pageIndex', pageIndex.toString())
params.append('pageSize', pageSize.toString())
params.append('assetType', assetType.toString())

if (isPublic != null) {
params.append('isPublic', isPublic.toString())
}
if (category) {
params.append('category', category)
}
if (isOrderByTime) {
params.append('isOrderByTime', '1')
}
if (isOrderByHot) {
params.append('isOrderByHot', '1')
}
if (author) {
params.append('author', author)
}

const url = `${baseAssetUrl}?${params.toString()}`
export type { IsPublic }

return service({
url: url,
method: 'get'
})
export enum AssetType {
Sprite = 0,
Backdrop = 1,
Sound = 2
}

/**
* Fetches a single asset
*
* @param id
* @returns Asset
*/
export function getAsset(id: number): Promise<Asset> {
const url = `/asset/${id}`
return service({
url: url,
method: 'get'
})
export type AssetData = {
// Globally unique ID
id: string
// Name to display
displayName: string
// Name of asset owner
owner: string
// Asset Category
category: string
// Public status
isPublic: IsPublic
// Files the asset contains
files: FileCollection
// Preview URL for the asset, e.g., a gif for a sprite
preview: string
// Asset Type
assetType: AssetType
// Click count of the asset
clickCount: number
}

/**
* @description: Search Asset by name.
* @param {string} search
* @param {number} assetType
* @return { SearchAssetResponse }
*/
export function searchAssetByName(
pageIndex: number,
pageSize: number,
search: string,
assetType: number
): Promise<PageAssetResponse> {
const baseAssetUrl = `/assets/search`
export type AddAssetParams = Pick<AssetData, 'displayName' | 'category' | 'isPublic' | 'files' | 'preview' | 'assetType'>

const params = new URLSearchParams()
params.append('pageIndex', pageIndex.toString())
params.append('pageSize', pageSize.toString())
params.append('search', search)
params.append('assetType', assetType.toString())
export function addAsset(params: AddAssetParams) {
return client.post('/asset', params) as Promise<AssetData>
}

const url = `${baseAssetUrl}?${params.toString()}`
export type UpdateAssetParams = AddAssetParams

return service({
url: url,
method: 'get'
})
export function updateAsset(id: string, params: UpdateAssetParams) {
return client.put(`/asset/${encodeURIComponent(id)}`, params) as Promise<AssetData>
}

/**
* Save asset
*
* @param id
* @param name
* @param uid
* @param category
* @param isPublic
* @param assetType The type of the asset. See src/constant/constant.ts for details.
* @param file
*/
export async function saveAsset(
id: number,
name: string,
uid: number,
category: string,
isPublic: number,
assetType: number,
file: File
): Promise<Asset> {
const url = '/asset/save'
const formData = new FormData()
formData.append('id', id.toString())
formData.append('name', name)
formData.append('uid', uid.toString())
formData.append('category', category)
formData.append('isPublic', isPublic ? '1' : '0')
formData.append('assetType', assetType.toString())
formData.append('file', file)

return service({
url: url,
method: 'post',
data: formData,
headers: {
'Content-Type': 'multipart/form-data'
}
})
export function deleteAsset(id: string) {
return client.delete(`/asset/${encodeURIComponent(id)}`) as Promise<void>
}

/**
* @description: Add asset click count.
* @param id
* @param assetType The type of the asset. See src/constant/constant.ts for details.
* @return {Promise<AxiosResponse<ResponseData<string>>>}
*/
export function addAssetClickCount(id: number): Promise<AxiosResponse<ResponseData<string>>> {
const url = `/asset/${id}/click-count`
return service({
url: url,
method: 'post'
})
export enum ListAssetParamOrderBy {
Default = 'default',
TimeDesc = 'time',
ClickCountDesc = 'clickCount'
}

/**
* @description: Publish asset to library.
* @param { string } name - sprite name named by user.
* @param { File[] } files - sprite costumes files, saved to show in lib.
* @param { string } assetType - sprite assetType, 0: sprite, 1: backdrop, 2: sound
* @param { PublishState } publishState - The publishing state of the asset. -1: not publish, 0: private lib, 1: public lib.
* @param { string|undefined } [gif] - Optional. The address of the sprite's GIF.
* Only provide this parameter if there is more than one file.
* It is used to display in the library when the sprite is hovering.
* @param { string|undefined } category - the category of the sprite(used to classify in library).
* @return { Promise<string> } - The result of the publishing operation.
*/
export function publishAsset(
name: string,
files: File[],
assetType: number,
publishState: PublishState,
previewAddress?: string,
export type ListAssetParams = PaginationParams & {
keyword?: string
assetType?: AssetType
category?: string
): Promise<string> {
const url = `/asset`
const formData = new FormData()
formData.append('name', name)
formData.append('assetType', assetType.toString())
files.forEach((file) => {
formData.append('files', file)
})
if (previewAddress) {
formData.append('previewAddress', previewAddress)
}
if (category) {
formData.append('category', category)
}
formData.append('publishState', publishState.toString())
isPublic?: IsPublic
owner?: string
orderBy?: ListAssetParamOrderBy
}

// Assume `service` is a predefined function for handling HTTP requests.
return service({
url: url,
method: 'post',
data: formData,
headers: {
'Content-Type': 'multipart/form-data'
}
})
export function listAsset(params?: ListAssetParams) {
return client.get('/assets/list', params) as Promise<ByPage<AssetData>>
}

/**
* @description: generate gif by costumes files
* @param {File} files - sprite costumes files
* @return {string} get sprites gif address.
*/
export function generateGifByCostumes(files: File[]): Promise<AxiosResponse<ResponseData<string>>> {
const url = `/util/to-gif`
const formData = new FormData()
files.forEach((file) => {
formData.append('files', file)
})
export function getAsset(id: string) {
return client.get(`/asset/${encodeURIComponent(id)}`) as Promise<AssetData>
}

return service({
url: url,
method: 'post',
data: formData,
headers: {
'Content-Type': 'multipart/form-data'
}
})
export function increaseAssetClickCount(id: string) {
return client.post(`/asset/${encodeURIComponent(id)}/click`) as Promise<void>
}
Loading

0 comments on commit bd972b5

Please sign in to comment.