@@ -183,14 +183,29 @@ export class CreatePostAction extends OperationOffChain {
183
183
const metadataId = uuid ( )
184
184
185
185
const { imageUrl } = inputs
186
- let imageMimeType = imageUrl ? `image/${ imageUrl . split ( '.' ) . pop ( ) } ` : null
187
- if ( ! imageMimeType || ! [ 3 , 4 ] . includes ( imageMimeType . length ) ) {
188
- imageMimeType = imageUrl ? 'image/jpeg' : null
186
+ let imageUrls : string [ ] = [ ]
187
+ if ( imageUrl && typeof imageUrl === 'string' ) {
188
+ imageUrls = [ imageUrl ]
189
+ } else if ( Array . isArray ( imageUrl ) ) {
190
+ imageUrls = imageUrl
189
191
}
190
192
193
+ // up to 1 non IPFS image is allowed
194
+ if ( imageUrls . length > 1 && imageUrls . some ( ( imageUrl ) => ! imageUrl . startsWith ( 'ipfs://' ) ) ) {
195
+ throw new Error ( 'Maximum of 1 image is allowed' )
196
+ }
197
+
198
+ const imageMimeTypes = imageUrls . map ( ( imageUrl ) => {
199
+ let imageMimeType = imageUrl ? `image/${ imageUrl . split ( '.' ) . pop ( ) } ` : null
200
+ if ( ! imageMimeType || ! [ 3 , 4 ] . includes ( imageMimeType . length ) ) {
201
+ imageMimeType = imageUrl ? 'image/jpeg' : null
202
+ }
203
+ return imageMimeType
204
+ } )
205
+
191
206
// upload image to IPFS
192
- let imageIpfs : string | undefined
193
- if ( imageUrl ) {
207
+ let ipfsImages : string [ ] = [ ]
208
+ if ( imageUrls . length && ! imageUrls [ 0 ] . startsWith ( 'ipfs://' ) ) {
194
209
const uploadFileUrl = `https://api.apireum.com/v1/ipfs/upload-file-url?key=${ process . env . APIREUM_API_KEY } `
195
210
const res = await axios . post ( uploadFileUrl , {
196
211
url : imageUrl ,
@@ -199,7 +214,9 @@ export class CreatePostAction extends OperationOffChain {
199
214
if ( ! res ?. data ?. file ?. cid ) {
200
215
throw new Error ( 'Failed to upload image to IPFS' )
201
216
}
202
- imageIpfs = `ipfs://${ res . data . file . cid } `
217
+ ipfsImages = [ `ipfs://${ res . data . file . cid } ` ]
218
+ } else {
219
+ ipfsImages = imageUrls
203
220
}
204
221
205
222
const data = {
@@ -208,22 +225,18 @@ export class CreatePostAction extends OperationOffChain {
208
225
description : content ,
209
226
content,
210
227
external_url : credentials . handle ? `https://lenster.xyz/u/${ credentials . handle } ` : 'https://chainjet.io' ,
211
- image : imageIpfs || null ,
212
- imageMimeType,
228
+ image : ipfsImages [ 0 ] || null ,
229
+ imageMimeType : imageMimeTypes [ 0 ] ,
213
230
name : credentials . handle ? `New Post by @${ credentials . handle } ` : 'New Post' ,
214
231
tags : ( content . match ( / # [ a - z A - Z 0 - 9 ] + / g) ?? [ ] ) . map ( ( tag : string ) => tag . slice ( 1 ) ) ,
215
- mainContentFocus : imageIpfs ? 'IMAGE' : 'TEXT_ONLY' ,
232
+ mainContentFocus : ipfsImages . length ? 'IMAGE' : 'TEXT_ONLY' ,
216
233
contentWarning : null ,
217
234
attributes : [ { traitType : 'type' , displayType : 'string' , value : 'post' } ] ,
218
- media : imageIpfs
219
- ? [
220
- {
221
- item : imageIpfs ,
222
- type : imageMimeType ,
223
- altTag : '' ,
224
- } ,
225
- ]
226
- : [ ] ,
235
+ media : ipfsImages . map ( ( imageUrl , index ) => ( {
236
+ item : imageUrl ,
237
+ type : imageMimeTypes [ index ] ,
238
+ altTag : '' ,
239
+ } ) ) ,
227
240
locale : 'en-US' ,
228
241
createdOn : new Date ( ) . toISOString ( ) ,
229
242
appId : 'ChainJet' ,
0 commit comments