azury
Version:
Powerful Javascript SDK for Azury
169 lines (137 loc) • 4.41 kB
JavaScript
import { GET, PATCH } from '../core/fetcher.js'
import { error } from '../core/logger.js'
// ―――――――――――――――――――――――――――――――― READ-ONLY ―――――――――――――――――――――――――――――――― //
export async function getFile(id) {
const res = await GET(`/files/${id}`)
if (res.ok) {
return await res.json()
} else {
logging && await error('Unable to fetch file.')
return null
}
}
export async function getFileName(id) {
const res = await GET(`/files/${id}/name`)
if (res.ok) {
return await res.json()
} else {
logging && await error('Unable to fetch file.')
return null
}
}
export async function getFileNote() {
const res = await GET(`/files/${file}/note`)
if (res.ok) {
return await res.json()
} else {
logging && await error('Unable to fetch file.')
return null
}
}
export async function getFileDownloadLink(id) {
return `https://api.azury.gg/download/${id}`
}
export async function getFileSharableLink(id) {
return `https://azury.gg/d/${id}`
}
export async function getFileAuthor() {
const res = await GET(`/files/${file}/author`)
if (res.ok) {
return await res.json()
} else {
logging && await error('Unable to fetch file.')
return null
}
}
export async function getFileUploadDate() {
const res = await GET(`/files/${file}/uploaded_at`)
if (res.ok) {
return await res.json()
} else {
logging && await error('Unable to fetch file.')
return null
}
}
export async function getFileUploadTimestamp() {
const res = await GET(`/files/${file}/upload_timestamp`)
if (res.ok) {
return await res.json()
} else {
logging && await error('Unable to fetch file.')
return null
}
}
export async function getFileSize() {
const res = await GET(`/files/${file}/size`)
if (res.ok) {
return await res.json()
} else {
logging && await error('Unable to fetch file.')
return null
}
}
export async function getReadableFileSize() {
const res = await GET(`/files/${file}/size`)
if (res.ok) {
const json = await res.json()
const length = json.length
const round = async (value, decimalPlaces) => {
value = Math.round(value + 'e' + decimalPlaces)
return Number(value + 'e' + -decimalPlaces)
}
if (length < 4) return `${size} B`
if (length >= 4 && length < 7) return `${await round(size / 1000, 2)} KB`
if (length >= 7 && length < 10) return `${await round(size / 1000000, 2)} MB`
if (length >= 10 && length < 13) return `${await round(size / 1000000000, 2)} GB`
if (length >= 13) return `${await round(size / 1000000000000, 2)} TB`
} else {
logging && await error('Unable to fetch file.')
return null
}
}
export async function getFileMimetype() {
const res = await GET(`/files/${file}/mimetype`)
if (res.ok) {
return await res.json()
} else {
logging && await error('Unable to fetch file.')
return null
}
}
export async function getFileVisibility() {
const res = await GET(`/files/${file}/visibility`)
if (res.ok) {
return await res.json()
} else {
logging && await error('Unable to fetch file.')
return null
}
}
// ―――――――――――――――――――――――――――――――― READ AND WRITE ―――――――――――――――――――――――――――――――― //
export async function changeFileName(id, name) {
const res = await PATCH(`/files/${id}/name`, { name: name })
if (res.ok) {
return await res.json()
} else {
logging && await error('Unable to fetch file.')
return null
}
}
export async function changeFileNote(id, note) {
const res = await PATCH(`/files/${id}/note`, { note: note })
if (res.ok) {
return await res.json()
} else {
logging && await error('Unable to fetch file.')
return null
}
}
export async function changeFileVisibility(id, visibility) {
const res = await PATCH(`/files/${id}/visibility`, { visibility: visibility })
if (res.ok) {
return await res.json()
} else {
logging && await error('Unable to fetch file.')
return null
}
}