@@ -183,14 +183,29 @@ export class CreatePostAction extends OperationOffChain {
183183 const metadataId = uuid ( )
184184
185185 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
189191 }
190192
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+
191206 // upload image to IPFS
192- let imageIpfs : string | undefined
193- if ( imageUrl ) {
207+ let ipfsImages : string [ ] = [ ]
208+ if ( imageUrls . length && ! imageUrls [ 0 ] . startsWith ( 'ipfs://' ) ) {
194209 const uploadFileUrl = `https://api.apireum.com/v1/ipfs/upload-file-url?key=${ process . env . APIREUM_API_KEY } `
195210 const res = await axios . post ( uploadFileUrl , {
196211 url : imageUrl ,
@@ -199,7 +214,9 @@ export class CreatePostAction extends OperationOffChain {
199214 if ( ! res ?. data ?. file ?. cid ) {
200215 throw new Error ( 'Failed to upload image to IPFS' )
201216 }
202- imageIpfs = `ipfs://${ res . data . file . cid } `
217+ ipfsImages = [ `ipfs://${ res . data . file . cid } ` ]
218+ } else {
219+ ipfsImages = imageUrls
203220 }
204221
205222 const data = {
@@ -208,22 +225,18 @@ export class CreatePostAction extends OperationOffChain {
208225 description : content ,
209226 content,
210227 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 ] ,
213230 name : credentials . handle ? `New Post by @${ credentials . handle } ` : 'New Post' ,
214231 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' ,
216233 contentWarning : null ,
217234 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+ } ) ) ,
227240 locale : 'en-US' ,
228241 createdOn : new Date ( ) . toISOString ( ) ,
229242 appId : 'ChainJet' ,
0 commit comments