@longviewlabs/strapi-provider-upload-arweave
Version:
Arweave upload provider for Strapi v5
169 lines (164 loc) • 5.9 kB
JavaScript
import { TurboFactory } from '@ardrive/turbo-sdk';
import z$1, { z } from 'zod';
z.object({
name: z.string(),
alternativeText: z.string().optional(),
caption: z.string().optional(),
width: z.number().optional(),
height: z.number().optional(),
formats: z.record(z.string(), z.unknown()).optional(),
hash: z.string(),
ext: z.string().optional(),
mime: z.string(),
size: z.number(),
sizeInBytes: z.number(),
url: z.string(),
previewUrl: z.string().optional(),
path: z.string().optional(),
provider: z.string().optional(),
provider_metadata: z.record(z.string(), z.unknown()).optional(),
stream: z.unknown().optional(),
buffer: z.unknown().optional()
});
const jwkSchema = z.object({
"kty": z.string({
error: "Invalid JWK format. Please check the content of your Arweave Wallet JWK"
}),
"e": z.string({
error: "Invalid JWK format. Please check the content of your Arweave Wallet JWK"
}),
"n": z.string({
error: "Invalid JWK format. Please check the content of your Arweave Wallet JWK"
}),
"d": z.string().optional(),
"dp": z.string().optional(),
"dq": z.string().optional(),
"ext": z.boolean().optional(),
"p": z.string().optional(),
"q": z.string().optional(),
"qi": z.string().optional()
});
const defaultGetContentType = (file)=>file.mime;
const defaultGateway = 'https://arweave.net';
const optionsSchema = z.object({
arweaveWallet: z.preprocess((input)=>{
if (typeof input === 'string') {
try {
return JSON.parse(input);
} catch {
throw new Error('Arweave Wallet provided is not a valid JSON. Please ensure it is a valid JWK format.');
}
}
return input;
}, jwkSchema),
gateway: z.url().default(defaultGateway),
getContentType: z.custom((val)=>typeof val === 'function').optional().default(()=>defaultGetContentType)
});
const getConfigDefaultValues = (config)=>{
try {
return optionsSchema.parse(config);
} catch (err) {
if (err instanceof z$1.ZodError) {
throw new Error(err.issues[0]?.message);
} else {
throw err;
}
}
};
const prepareUploadFile = async (file, config)=>{
let fileBuffer;
if (file.stream) {
const buffers = [];
for await (const chunk of file.stream){
buffers.push(chunk);
}
fileBuffer = Buffer.concat(buffers);
} else if (file.buffer) {
fileBuffer = file.buffer;
} else {
throw new Error('File must have either a stream or a buffer.');
}
const fileSize = fileBuffer.length;
const fileContentType = config.getContentType(file);
return {
fileBuffer,
fileSize,
fileContentType
};
};
var index = {
init (providedConfig) {
const config = getConfigDefaultValues(providedConfig);
const { arweaveWallet } = config;
const turbo = TurboFactory.authenticated({
privateKey: arweaveWallet
});
// const arweave = Arweave.init({});
// let jwk = arweaveWallet;
// if (!jwk) {
// console.log(__dirname);
// try {
// const arweaveJson = fs.readFileSync('arweave.json', 'utf-8');
// jwk = JSON.parse(arweaveJson);
// } catch (error) {
// // If reading the file failed, log the error
// console.error('Error reading arweave.json:', error);
// }
// }
// if (!jwk) {
// throw new Error('Arweave wallet JSON is required. Please set the arweaveWallet option, or have wallet as "arweave.json" file in the project root.');
// }
const upload = async (file)=>{
try {
const { fileBuffer , fileSize , fileContentType } = await prepareUploadFile(file, config);
const res = await turbo.uploadFile({
fileStreamFactory: ()=>fileBuffer,
fileSizeFactory: ()=>fileSize,
dataItemOpts: {
tags: [
{
name: "Content-Type",
value: fileContentType
}
]
}
});
file.url = `${res.id}`;
} catch (error) {
if (error.status === 402) {
const message = `Insufficient balance. Please follow the documentation on how to top up your ArDrive Turbo account (https://github.com/longview-labs/strapi-provider-upload-arweave?tab=readme-ov-file#how-to-pay-for-uploads)`;
console.error(message);
throw new Error(message);
} else if (error instanceof Error && 'message' in error) {
const message1 = `Error uploading file to Arweave: ${error.message}`;
console.error(message1);
throw new Error(message1);
} else {
throw error;
}
}
};
return {
async upload (file) {
return upload(file);
},
async uploadStream (file) {
return upload(file);
},
async delete (file) {
return "Files save on Arweave cannot be deleted as they are immutable.";
},
isPrivate () {
return true;
},
async getSignedUrl (file) {
// transform url to specific gateway
return {
url: `${config.gateway}/${file.url}`
};
}
};
}
};
export { index as default };
//# sourceMappingURL=index.mjs.map