Skip to content

Commit

Permalink
fallback image uploader renamed to image kit uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
tempe-techie committed Sep 15, 2024
1 parent c215f44 commit ca26701
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 47 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ ARWEAVE_ADDRESS=

Also make sure these variables are set on your hosting provider (Netlify, Vercel, etc).

## Image upload fallback
## ImageKit upload

It is recommended to use ImageKit as the fallback option, in case Arweave has technical issues.

Expand All @@ -102,6 +102,8 @@ IMAGEKIT_PUBLIC_KEY=
IMAGEKIT_PRIVATE_KEY=
```

You can also use ImageKit as your main image upload, if you set the `fileUploadStorageType` variable in nuxt config to "imagekit".

## Customize

- Project-specific settings in `nuxt.config.ts`
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion components/chat/ChatFeed.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
@processFileUrl="insertImage"
title="Upload image"
infoText="Upload an image."
storageType="arweave"
:storageType="$config.fileUploadStorageType"
:componentId="$.uid"
:maxFileSize="$config.fileUploadSizeLimit"
/>
Expand Down
2 changes: 1 addition & 1 deletion components/nft/collection/AddImageToCollectionModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<FileUploadInput
btnCls="btn btn-primary"
storageType="arweave"
:storageType="$config.fileUploadStorageType"
:maxFileSize="$config.fileUploadSizeLimit"
@processUploadedFileUrl="insertImageLink"
/>
Expand Down
2 changes: 1 addition & 1 deletion components/nft/collection/ChangeCollectionPreviewModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

<FileUploadInput
btnCls="btn btn-primary"
storageType="arweave"
:storageType="$config.fileUploadStorageType"
:maxFileSize="$config.fileUploadSizeLimit"
@processUploadedFileUrl="insertImageLink"
/>
Expand Down
2 changes: 1 addition & 1 deletion components/profile/PunkProfile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
@processFileUrl="insertImage"
title="Change profile image"
infoText="Upload a new profile picture."
storageType="arweave"
:storageType="$config.fileUploadStorageType"
:componentId="$.uid"
:maxFileSize="$config.fileUploadSizeLimit"
/>
Expand Down
89 changes: 48 additions & 41 deletions components/storage/FileUploadInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,51 @@ export default {
},
methods: {
async arweaveUpload() {
const thisAppUrl = window.location.origin
let fetcherService
if (this.$config.fileUploadTokenService === 'netlify') {
fetcherService = thisAppUrl + '/.netlify/functions/arweaveUploader'
} else if (this.$config.fileUploadTokenService === 'vercel') {
fetcherService = thisAppUrl + '/api/arweaveUploader'
}
// Convert file to base64
const fileData = await this.fileToBase64(this.file)
const fileType = this.file.type
const resp = await axios.post(fetcherService, {
fileData,
fileName: this.file.name,
fileType: this.file.type
})
const transactionId = resp.data.transactionId
let fileUri = `ar://${transactionId}`
// add file type to file uri so we can use it in the frontend
if (fileType.startsWith('image/')) {
fileUri += `?img`
} else if (fileType.startsWith('video/') || fileType.startsWith('audio/')) {
fileUri += `?${fileType}`
} else if (fileType.startsWith('text/plain')) {
fileUri += `?txt`
}
// emit file url
this.$emit('processUploadedFileUrl', fileUri)
},
async imageKitUpload() {
const thisAppUrl = window.location.origin
let fetcherService
if (this.$config.fileUploadTokenService === 'netlify') {
fetcherService = thisAppUrl + '/.netlify/functions/imageUploaderFallback'
fetcherService = thisAppUrl + '/.netlify/functions/imageKitUploader'
} else if (this.$config.fileUploadTokenService === 'vercel') {
fetcherService = thisAppUrl + '/api/imageUploaderFallback'
fetcherService = thisAppUrl + '/api/imageKitUploader'
}
const resp = await $fetch(fetcherService).catch(error => error.data)
Expand Down Expand Up @@ -118,51 +155,21 @@ export default {
if (this.storageType === 'arweave') {
try {
const thisAppUrl = window.location.origin
let fetcherService
if (this.$config.fileUploadTokenService === 'netlify') {
fetcherService = thisAppUrl + '/.netlify/functions/arweaveUploader'
} else if (this.$config.fileUploadTokenService === 'vercel') {
fetcherService = thisAppUrl + '/api/arweaveUploader'
}
console.log(fetcherService)
// Convert file to base64
const fileData = await this.fileToBase64(this.file)
const fileType = this.file.type
const resp = await axios.post(fetcherService, {
fileData,
fileName: this.file.name,
fileType: this.file.type
})
console.log(resp)
const transactionId = resp.data.transactionId
let fileUri = `ar://${transactionId}`
// add file type to file uri so we can use it in the frontend
if (fileType.startsWith('image/')) {
fileUri += `?img`
} else if (fileType.startsWith('video/') || fileType.startsWith('audio/')) {
fileUri += `?${fileType}`
} else if (fileType.startsWith('text/plain')) {
fileUri += `?txt`
}
// emit file url
this.$emit('processUploadedFileUrl', fileUri)
await this.arweaveUpload()
} catch (error) {
console.error('Error uploading file to decentralized storage service', error)
console.log('Falling back to centralized storage service')
await this.imageKitUpload()
}
} else {
// use centralized storage service
await this.imageKitUpload()
// use centralized storage service ImageKit
try {
await this.imageKitUpload()
} catch (error) {
console.error('Error uploading file to centralized storage service', error)
console.log('Falling back to decentralized storage service')
await this.arweaveUpload()
}
}
this.waitingUpload = false
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default defineNuxtConfig({
favicon: '/img/favicon.svg',
fileUploadEnabled: true, // enable/disable file uploads (enable only if external file storage is used, e.g. Arweave)
fileUploadSizeLimit: 1 * 1024 * 1024, // max file upload size in bytes (1 * 1024 * 1024 = 1 MB)
fileUploadStorageType: "arweave", // "arweave" or "imagekit"
fileUploadTokenService: process.env.FILE_UPLOAD_SERVICE || 'netlify', // "netlify" or "vercel" (or leave empty for no file uploads)
getPostsLimit: 30, // number of posts to fetch from Orbis in the getPosts() function
governanceUrl: 'https://snapshot.org/#/sgbchat.eth', // governance url (snapshot, Tally, etc.)
Expand Down
2 changes: 1 addition & 1 deletion pages/nft/create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
@processFileUrl="insertImage"
title="Upload your NFT image"
infoText="Upload the NFT image."
storageType="arweave"
:storageType="$config.fileUploadStorageType"
:componentId="$.uid"
:maxFileSize="$config.fileUploadSizeLimit"
/>
Expand Down

0 comments on commit ca26701

Please sign in to comment.