The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Add file size validation for storage explorer when uploading files (#23190)
Joshen Lime1b6db4b4380
diff --git a/apps/studio/components/layouts/StorageLayout/StorageMenu.tsx b/apps/studio/components/layouts/StorageLayout/StorageMenu.tsxindex 73f4a8be2d4..3a872ec1545 100644--- a/apps/studio/components/layouts/StorageLayout/StorageMenu.tsx+++ b/apps/studio/components/layouts/StorageLayout/StorageMenu.tsx@@ -13,14 +13,8 @@ import { EmptyBucketModal } from 'components/to-be-cleaned/Storage/EmptyBucketMoimport ShimmeringLoader from 'components/ui/ShimmeringLoader'import { useBucketsQuery } from 'data/storage/buckets-query'import { useCheckPermissions, useSelectedProject } from 'hooks'-import {- AlertDescription_Shadcn_,- AlertTitle_Shadcn_,- Alert_Shadcn_,- Button,- IconEdit,- Menu,-} from 'ui'+import { ArrowUpRight, Edit } from 'lucide-react'+import { AlertDescription_Shadcn_, AlertTitle_Shadcn_, Alert_Shadcn_, Button, Menu } from 'ui'import BucketRow from './BucketRow'const StorageMenu = () => {@@ -56,8 +50,8 @@ const StorageMenu = () => {blocktype="default"icon={- <div className="text-foreground-lighter">- <IconEdit size={14} />+ <div className="text-foreground-light">+ <Edit size={14} /></div>}disabled={!canCreateBuckets}@@ -152,6 +146,14 @@ const StorageMenu = () => {<p className="truncate">Policies</p></Menu.Item></Link>+ <Link href={`/project/${ref}/settings/storage`}>+ <Menu.Item rounded>+ <div className="flex items-center justify-between">+ <p className="truncate">Settings</p>+ <ArrowUpRight strokeWidth={1} className="h-4 w-4" />+ </div>+ </Menu.Item>+ </Link></div></div></Menu>diff --git a/apps/studio/components/to-be-cleaned/Storage/StorageExplorer/StorageExplorer.tsx b/apps/studio/components/to-be-cleaned/Storage/StorageExplorer/StorageExplorer.tsxindex 9c6f0f2be6b..95e4943af50 100644--- a/apps/studio/components/to-be-cleaned/Storage/StorageExplorer/StorageExplorer.tsx+++ b/apps/studio/components/to-be-cleaned/Storage/StorageExplorer/StorageExplorer.tsx@@ -7,7 +7,7 @@ import toast from 'react-hot-toast'import { useProjectSettingsQuery } from 'data/config/project-settings-query'import { useCustomDomainsQuery } from 'data/custom-domains/custom-domains-query'import type { Bucket } from 'data/storage/buckets-query'-import { DEFAULT_PROJECT_API_SERVICE_ID } from 'lib/constants'+import { DEFAULT_PROJECT_API_SERVICE_ID, IS_PLATFORM } from 'lib/constants'import { copyToClipboard } from 'lib/helpers'import { useStorageStore } from 'localStores/storageExplorer/StorageExplorerStore'import { STORAGE_ROW_TYPES, STORAGE_VIEWS } from '../Storage.constants'@@ -18,6 +18,7 @@ import FileExplorerHeader from './FileExplorerHeader'import FileExplorerHeaderSelection from './FileExplorerHeaderSelection'import MoveItemsModal from './MoveItemsModal'import PreviewPane from './PreviewPane'+import { useProjectStorageConfigQuery } from 'data/config/project-storage-config-query'interface StorageExplorerProps {bucket: Bucket@@ -58,6 +59,11 @@ const StorageExplorer = ({ bucket }: StorageExplorerProps) => {const { ref } = useParams()const { data: customDomainData } = useCustomDomainsQuery({ projectRef: ref })const { data: projectSettings } = useProjectSettingsQuery({ projectRef: ref })++ // [Joshen] This is to ensure that StorageExplorerStore can get the storage file size limit+ // Will be better once we deprecate the mobx store entirely, which we will get there+ useProjectStorageConfigQuery({ projectRef: ref }, { enabled: IS_PLATFORM })+const apiService = (projectSettings?.services ?? []).find((x) => x.app.id == DEFAULT_PROJECT_API_SERVICE_ID)diff --git a/apps/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx b/apps/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsxindex 660819eb671..4bbde7e7e4b 100644--- a/apps/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx+++ b/apps/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx@@ -30,7 +30,7 @@ const StorageSettings = () => {isError,} = useProjectStorageConfigQuery({ projectRef }, { enabled: IS_PLATFORM })const { fileSizeLimit, isFreeTier } = config || {}- const { value, unit } = convertFromBytes(fileSizeLimit)+ const { value, unit } = convertFromBytes(fileSizeLimit ?? 0)const [selectedUnit, setSelectedUnit] = useState(unit)let initialValues = { fileSizeLimit: value, unformattedFileSizeLimit: fileSizeLimit }diff --git a/apps/studio/data/config/project-storage-config-query.ts b/apps/studio/data/config/project-storage-config-query.tsindex 8408f0e74d9..ce8f0a09dbe 100644--- a/apps/studio/data/config/project-storage-config-query.ts+++ b/apps/studio/data/config/project-storage-config-query.ts@@ -1,32 +1,29 @@import { useQuery, UseQueryOptions } from '@tanstack/react-query'-import { get } from 'lib/common/fetch'+import { get, handleError } from 'data/fetchers'import { API_URL } from 'lib/constants'import { configKeys } from './keys'import type { ResponseError } from 'types'+import { components } from 'data/api'export type ProjectStorageConfigVariables = {projectRef?: string}-// TODO: Add proper type-export type ProjectStorageConfigResponse = any+export type ProjectStorageConfigResponse = components['schemas']['StorageConfigResponse']export async function getProjectStorageConfig({ projectRef }: ProjectStorageConfigVariables,signal?: AbortSignal) {- if (!projectRef) {- throw new Error('projectRef is required')- }+ if (!projectRef) throw new Error('projectRef is required')- const response = await get(`${API_URL}/projects/${projectRef}/config/storage`, {+ const { data, error } = await get('/platform/projects/{ref}/config/storage', {+ params: { path: { ref: projectRef } },signal,})- if (response.error) {- throw response.error- }- return response as ProjectStorageConfigResponse+ if (error) throw handleError(error)+ return data}export type ProjectStorageConfigData = Awaited<ReturnType<typeof getProjectStorageConfig>>diff --git a/apps/studio/localStores/storageExplorer/StorageExplorerStore.tsx b/apps/studio/localStores/storageExplorer/StorageExplorerStore.tsxindex 88f72d9052e..cd68f041666 100644--- a/apps/studio/localStores/storageExplorer/StorageExplorerStore.tsx+++ b/apps/studio/localStores/storageExplorer/StorageExplorerStore.tsx@@ -16,6 +16,13 @@ import { ToastLoader } from 'components/ui/ToastLoader'import { delete_, post } from 'lib/common/fetch'import { API_URL, IS_PLATFORM } from 'lib/constants'import { PROJECT_ENDPOINT_PROTOCOL } from 'pages/api/constants'+import { getQueryClient } from 'data/query-client'+import { configKeys } from 'data/config/keys'+import { ProjectStorageConfigResponse } from 'data/config/project-storage-config-query'+import {+ convertFromBytes,+ convertToBytes,+} from 'components/to-be-cleaned/Storage/StorageSettings/StorageSettings.utils'/*** This is a preferred method rather than React Context and useStorageExplorerStore().@@ -459,6 +466,14 @@ class StorageExplorerStore {}uploadFiles = async (files, columnIndex, isDrop = false) => {+ const queryClient = getQueryClient()+ const storageConfiguration = queryClient+ .getQueryCache()+ .find(configKeys.storage(this.projectRef))?.state.data as+ | ProjectStorageConfigResponse+ | undefined+ const fileSizeLimit = storageConfiguration?.fileSizeLimit+const t1 = new Date()const autofix = true@@ -468,12 +483,39 @@ class StorageExplorerStore {: Array.from(files)const derivedColumnIndex = columnIndex === -1 ? this.getLatestColumnIndex() : columnIndex+ const filesWithinUploadLimit =+ fileSizeLimit !== undefined+ ? filesToUpload.filter((file) => file.size <= fileSizeLimit)+ : filesToUpload++ if (filesWithinUploadLimit.length < filesToUpload.length) {+ const numberOfFilesRejected = filesToUpload.length - filesWithinUploadLimit+ const { value, unit } = convertFromBytes(fileSizeLimit)++ toast.error(+ <div className="flex flex-col gap-y-1">+ <p className="text-foreground">+ Failed to upload {numberOfFilesRejected} file{numberOfFilesRejected > 1 ? 's' : ''} as{' '}+ {numberOfFilesRejected > 1 ? 'their' : 'its'} size+ {numberOfFilesRejected > 1 ? 's are' : ' is'} beyond the upload limit of {value}+ {unit}.+ </p>+ <p className="text-foreground-light">+ You may change the file size upload limit under Storage in Project Settings.+ </p>+ </div>,+ { duration: 8000 }+ )++ if (numberOfFilesRejected === filesToUpload.length) return+ }+// If we're uploading a folder which name already exists in the same folder that we're uploading to// We sanitize the folder name and let all file uploads through. (This is only via drag drop)const topLevelFolders = (this.columns?.[derivedColumnIndex]?.items ?? []).filter((item) => !item.id).map((item) => item.name)- const formattedFilesToUpload = filesToUpload.map((file) => {+ const formattedFilesToUpload = filesWithinUploadLimit.map((file) => {// If the files are from clicking "Upload button", just take them as they are since users cannot// upload folders from clicking that button, only via drag dropif (!file.path) return file