Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#4) Added option to Include File URL in schema. #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/core/src/components/Uploader/Uploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export interface UploaderProps {
* Whether or not we should use the file's name when uploading
*/
storeOriginalFilename?: boolean
/**
* Whether or not to include the file URL in the Sanity document. This is useful if file URLs needs to be validated before fetched. This defaults to true.
*/
includeFileURL?: boolean

// FIELD INPUT CONTEXT
/**
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/components/Uploader/useUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const useUpload = ({
vendorConfig,
sanityClient,
storeOriginalFilename = true,
includeFileURL = true,
onSuccess,
}: UploaderProps): useUploadReturn => {
const toast = useToast()
Expand All @@ -51,6 +52,7 @@ const useUpload = ({
file: context.file as File,
storeOriginalFilename,
}),
includeFileURL,
onError: (error) =>
callback({
type: 'VENDOR_ERROR',
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export interface VendorConfiguration {
* Use this instead of file.name as it respects users' storeOriginalFilename options.
*/
fileName: string
/**
* Whether or not to include the file URL in the Sanity document. This is useful if file URLs needs to be validated before fetched. This defaults to true.
*/
includeFileURL: boolean
/**
* Credentials as configured by your plugin.
*/
Expand Down
8 changes: 6 additions & 2 deletions packages/firebase/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const config: VendorConfiguration = {
onSuccess,
file,
fileName,
includeFileURL,
updateProgress,
}) => {
const firebaseClient = getFirebaseClient(credentials as FirebaseCredentials)
Expand All @@ -76,11 +77,14 @@ const config: VendorConfiguration = {
onError(error)
},
async () => {
const downloadURL = await uploadTask.snapshot.ref.getDownloadURL()
let downloadURL: string | null = null
if (includeFileURL) {
downloadURL = await uploadTask.snapshot.ref.getDownloadURL()
}
const metadata = await uploadTask.snapshot.ref.getMetadata()

onSuccess({
fileURL: downloadURL,
fileURL: includeFileURL ? downloadURL : null,
firebase: {
bucket: metadata.bucket,
contentDisposition: metadata.contentDisposition,
Expand Down