feat: add album and media management features, including album creation, media upload, and routing
This commit is contained in:
56
libertas-frontend/src/services/media-service.ts
Normal file
56
libertas-frontend/src/services/media-service.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import type { Media, PaginatedResponse } from "@/domain/types"
|
||||
import apiClient from "@/services/api-client"
|
||||
|
||||
type MediaListParams = {
|
||||
page: number
|
||||
limit: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a paginated list of media.
|
||||
*/
|
||||
export const getMediaList = async ({
|
||||
page,
|
||||
limit,
|
||||
}: MediaListParams): Promise<PaginatedResponse<Media>> => {
|
||||
const { data } = await apiClient.get('/media', {
|
||||
params: { page, limit },
|
||||
})
|
||||
|
||||
// we need to append base url to file_url and thumbnail_url
|
||||
const prefix = import.meta.env.VITE_PREFIX_PATH || apiClient.defaults.baseURL;
|
||||
|
||||
data.data = data.data.map((media: Media) => ({
|
||||
...media,
|
||||
file_url: `${prefix}${media.file_url}`,
|
||||
thumbnail_url: media.thumbnail_url
|
||||
? `${prefix}${media.thumbnail_url}`
|
||||
: null,
|
||||
}))
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads a new media file.
|
||||
*/
|
||||
export const uploadMedia = async (
|
||||
file: File,
|
||||
onProgress: (progress: number) => void,
|
||||
): Promise<Media> => {
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
|
||||
const { data } = await apiClient.post('/media', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
onUploadProgress: (progressEvent) => {
|
||||
const percentCompleted = Math.round(
|
||||
(progressEvent.loaded * 100) / (progressEvent.total ?? 100),
|
||||
)
|
||||
onProgress(percentCompleted)
|
||||
},
|
||||
})
|
||||
return data
|
||||
}
|
||||
Reference in New Issue
Block a user