Skip to content

Commit

Permalink
Merge branch 'alpha' of github.com:packagelabs/freshmint into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
psiemens committed Aug 29, 2022
2 parents a6b6177 + df08c22 commit cc8f44e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 56 deletions.
56 changes: 7 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"inquirer": "^8.2.2",
"js-yaml": "^4.1.0",
"nft.storage": "^3.3.0",
"node-fetch": "^2.6.7",
"ora": "^5.4.1",
"pouchdb": "^7.2.2",
"pouchdb-find": "^7.2.2",
Expand Down
38 changes: 31 additions & 7 deletions src/cli/processors/IPFSFileProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from 'path';
import * as fs from 'fs/promises';
import fetch from 'node-fetch';
import IPFS from '../ipfs';
import { metadata } from '../../lib';
import { FieldProcessor } from '.';
Expand All @@ -16,20 +17,43 @@ export default class IPFSFileProcessor implements FieldProcessor {
}

async process(value: metadata.MetadataValue): Promise<metadata.MetadataValue> {
const filename = value as string;
const data = await this.#readFile(value as string);

const fullPath = `${this.#nftAssetPath}/${filename}`;
const cid = await this.#ipfs.pin(data);

return cid;
}

async #readFile(value: string): Promise<Buffer> {
if (isURL(value)) {
return this.#readRemoteFile(value);
}

let data: Buffer;
return this.#readLocalFile(value);
}

async #readLocalFile(filename: string): Promise<Buffer> {
const fullPath = `${this.#nftAssetPath}/${filename}`;

try {
data = await fs.readFile(path.resolve(process.cwd(), fullPath));
return await fs.readFile(path.resolve(process.cwd(), fullPath));
} catch (e) {
throw new Error(`Failed to mint token, failed to read asset file at ${fullPath}`);
throw new Error(`Failed to read asset file at ${fullPath}`);
}
}

const cid = await this.#ipfs.pin(data);
async #readRemoteFile(url: string): Promise<Buffer> {
const response = await fetch(url);
const arrayBuffer = await response.arrayBuffer();
return Buffer.from(arrayBuffer);
}
}

return cid;
function isURL(value: string): boolean {
try {
new URL(value);
return true;
} catch {
return false;
}
}

0 comments on commit cc8f44e

Please sign in to comment.