-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add cloudinary fxn to partners (#3393)
- Loading branch information
1 parent
ed11180
commit 03cb224
Showing
2 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import axios from "axios" | ||
|
||
interface CloudinaryUploadProps { | ||
file: File | ||
onUploadProgress: (progress: number) => void | ||
cloudName: string | ||
uploadPreset: string | ||
tag?: string | ||
signature?: string | ||
apiKey?: string | ||
timestamp?: number | ||
} | ||
|
||
export const CloudinaryUpload = async ({ | ||
file, | ||
onUploadProgress, | ||
cloudName, | ||
uploadPreset, | ||
signature, | ||
apiKey, | ||
timestamp, | ||
tag = "browser_upload", | ||
}: CloudinaryUploadProps) => { | ||
const url = `https://api.cloudinary.com/v1_1/${cloudName}/upload` | ||
const data = new FormData() | ||
data.append("upload_preset", uploadPreset) | ||
data.append("tags", tag) | ||
data.append("file", file) | ||
if (signature && timestamp && apiKey) { | ||
data.append("signature", signature) | ||
data.append("timestamp", `${timestamp}`) | ||
data.append("api_key", apiKey) | ||
} | ||
|
||
if (!cloudName || cloudName == "" || !uploadPreset || uploadPreset == "") { | ||
const err = "Please supply a cloud name and upload preset for Cloudinary" | ||
alert(err) | ||
throw err | ||
} | ||
|
||
const response = await axios.request({ | ||
method: "post", | ||
url: url, | ||
data: data, | ||
onUploadProgress: (p) => { | ||
onUploadProgress(parseInt(((p.loaded / p.total) * 100).toFixed(0), 10)) | ||
}, | ||
}) | ||
return response | ||
} |