Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
feat(assets): remove attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed May 9, 2022
1 parent 4717b76 commit ef77b3c
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 171 deletions.
18 changes: 5 additions & 13 deletions docs/.vuepress/components/Assets.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,27 @@
<div class="asset-body">
<video
style="width: 100px; height: 100px"
:src="asset.attachments.find((attachment) => attachment.type === 'preview')?.address"
:src="asset.previews?.[0]?.address"
:fit="'cover'"
v-if="
asset.attachments
.find((attachment) => attachment.type === 'preview')
?.mime_type?.split('/')[0] === 'video'
"
v-if="asset.previews?.[0]?.mime_type?.split('/')[0] === 'video'"
autoplay
loop
muted
/>
<model-viewer
style="width: 100px; height: 100px"
:src="asset.attachments.find((attachment) => attachment.type === 'preview')?.address"
:src="asset.previews?.[0]?.address"
ar
ar-modes="webxr scene-viewer quick-look"
seamless-poster
shadow-intensity="1"
camera-controls
enable-pan
v-else-if="
asset.attachments
.find((attachment) => attachment.type === 'preview')
?.mime_type?.split('/')[0] === 'model'
"
v-else-if="asset.previews?.[0]?.mime_type?.split('/')[0] === 'model'"
></model-viewer>
<el-image
style="width: 100px; height: 100px"
:src="asset.attachments.find((attachment) => attachment.type === 'preview')?.address"
:src="asset.previews?.[0]?.address"
:fit="'cover'"
v-else
/>
Expand Down
20 changes: 16 additions & 4 deletions docs/guide/assets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,27 @@ type Assets = {
owners: AccountInstanceURI[];
name?: string;
description?: string;
attachments?: {
type?: string;

previews?: {
content?: string;
address?: URI;
mime_type: string;
mime_type?: string;
size_in_bytes?: number;
}[];

source: AssetSource | NoteSource;
items?: {
content?: string;
address?: URI;
mime_type?: string;
size_in_bytes?: number;
}[];

attributes?: {
content?: string;
mime_type?: string;
}[];

source: AssetSource;

metadata?: {
network: Network;
Expand Down
39 changes: 26 additions & 13 deletions src/assets/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,33 @@ abstract class Base {
}
}

generateMimeType(asset: Asset) {
asset.attachments?.forEach((attachment) => {
if (attachment.address) {
const mimeType = mime.getType(attachment.address);
if (mimeType) {
attachment.mime_type = mimeType;
} else if (new URL(attachment.address).searchParams.get('ext')) {
const mimeType = mime.getType(new URL(attachment.address).searchParams.get('ext')!);
if (mimeType) {
attachment.mime_type = mimeType;
}
}
generateMimeType(address: string) {
const mimeType = mime.getType(address);
if (mimeType) {
return mimeType;
} else if (new URL(address).searchParams.get('ext')) {
const mimeType = mime.getType(new URL(address).searchParams.get('ext')!);
if (mimeType) {
return mimeType;
}
});
}
}

generateAttributes(attributes: any): Asset['attributes'] | undefined {
if (Array.isArray(attributes)) {
return attributes
.map((attribute: any) => {
if (attribute.trait_type && attribute.value) {
return {
key: attribute.trait_type,
value: attribute.value,
};
} else {
return null;
}
})
.filter((attribute: any) => attribute) as Asset['attributes'];
}
}
}

Expand Down
40 changes: 20 additions & 20 deletions src/assets/ethereum-nft-alchemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class EthereumNFTAlchemy extends Base {
owners: [options.identity],
name: item.title,
description: item.description,
attachments: [],

source: 'Ethereum NFT',

Expand All @@ -51,28 +50,31 @@ class EthereumNFTAlchemy extends Base {
},
};

if (item.metadata.image || item.metadata.image_url) {
asset.attachments!.push({
type: 'preview',
address: this.main.utils.replaceIPFS(item.metadata.image || item.metadata.image_url),
});
const preview = item.metadata.image || item.metadata.image_url;
if (preview) {
asset.previews = [
{
address: this.main.utils.replaceIPFS(preview),
mime_type: this.generateMimeType(preview),
},
];
}

if (item.metadata.animation_url || item.metadata.image || item.metadata.image_url) {
asset.attachments!.push({
type: 'object',
address: this.main.utils.replaceIPFS(
item.metadata.animation_url || item.metadata.image || item.metadata.image_url,
),
});
const infoItem = item.metadata.animation_url || item.metadata.image || item.metadata.image_url;
if (infoItem) {
asset.items = [
{
address: this.main.utils.replaceIPFS(infoItem),
mime_type: this.generateMimeType(infoItem),
},
];
}

if (item.metadata.attributes) {
asset.attachments!.push({
type: 'attributes',
content: JSON.stringify(item.metadata.attributes),
mime_type: 'text/json',
});
const attributes = this.generateAttributes(item.metadata.attributes);
if (attributes) {
asset.attributes = attributes;
}
}

this.generateRelatedUrls(asset);
Expand All @@ -84,8 +86,6 @@ class EthereumNFTAlchemy extends Base {
asset.related_urls.push(item.metadata.external_url || item.metadata.external_link);
}

this.generateMimeType(asset);

return asset;
});

Expand Down
40 changes: 20 additions & 20 deletions src/assets/ethereum-nft-moralis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class EthereumNFTMoralis extends Base {
owners: [item.owner_of || options.identity],
name: metadata?.name || `${item.name} #${item.token_id}`,
description: metadata?.description,
attachments: [],

source: 'Ethereum NFT',

Expand All @@ -99,28 +98,31 @@ class EthereumNFTMoralis extends Base {
},
};

if (metadata?.image || metadata?.image_url) {
asset.attachments!.push({
type: 'preview',
address: this.main.utils.replaceIPFS(metadata?.image || metadata?.image_url),
});
const preview = metadata?.image || metadata?.image_url;
if (preview) {
asset.previews = [
{
address: this.main.utils.replaceIPFS(preview),
mime_type: this.generateMimeType(preview),
},
];
}

if (metadata?.animation_url || metadata?.image || metadata?.image_url) {
asset.attachments!.push({
type: 'object',
address: this.main.utils.replaceIPFS(
metadata?.animation_url || metadata?.image || metadata?.image_url,
),
});
const infoItem = metadata?.animation_url || metadata?.image || metadata?.image_url;
if (infoItem) {
asset.items = [
{
address: this.main.utils.replaceIPFS(infoItem),
mime_type: this.generateMimeType(infoItem),
},
];
}

if (metadata?.attributes) {
asset.attachments!.push({
type: 'attributes',
content: JSON.stringify(metadata?.attributes),
mime_type: 'text/json',
});
const attributes = this.generateAttributes(metadata?.attributes);
if (attributes) {
asset.attributes = attributes;
}
}

this.generateRelatedUrls(asset);
Expand All @@ -132,8 +134,6 @@ class EthereumNFTMoralis extends Base {
asset.related_urls.push(metadata?.external_url || metadata?.external_link);
}

this.generateMimeType(asset);

return asset;
}),
);
Expand Down
32 changes: 16 additions & 16 deletions src/assets/ethereum-nft-opensea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class EthereumNFTOpensea extends Base {
owners: [item.owner.address],
name: item.name,
description: item.description,
attachments: [],

source: 'Ethereum NFT',

Expand All @@ -46,25 +45,28 @@ class EthereumNFTOpensea extends Base {
};

if (item.image_original_url) {
asset.attachments!.push({
type: 'preview',
address: this.main.utils.replaceIPFS(item.image_original_url),
});
asset.previews = [
{
address: this.main.utils.replaceIPFS(item.image_original_url),
mime_type: this.generateMimeType(item.image_original_url),
},
];
}

if (item.animation_original_url) {
asset.attachments!.push({
type: 'object',
address: this.main.utils.replaceIPFS(item.animation_original_url),
});
asset.items = [
{
address: this.main.utils.replaceIPFS(item.animation_original_url),
mime_type: this.generateMimeType(item.animation_original_url),
},
];
}

if (item.traits) {
asset.attachments!.push({
type: 'attributes',
content: JSON.stringify(item.traits),
mime_type: 'text/json',
});
const attributes = this.generateAttributes(item.traits);
if (attributes) {
asset.attributes = attributes;
}
}

this.generateRelatedUrls(asset);
Expand All @@ -76,8 +78,6 @@ class EthereumNFTOpensea extends Base {
asset.related_urls.push(item.external_link);
}

this.generateMimeType(asset);

return asset;
});

Expand Down

0 comments on commit ef77b3c

Please sign in to comment.