diff --git a/docker-compose.yml b/docker-compose.yml index 8032ae38..2a687505 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -218,5 +218,5 @@ services: networks: shared: ipv4_address: 10.10.10.59 - ports: - - "27017:27017" + expose: + - 27017 diff --git a/packages/ipfs-api/.prettierignore b/packages/ipfs-api/.prettierignore new file mode 100644 index 00000000..020a4cbb --- /dev/null +++ b/packages/ipfs-api/.prettierignore @@ -0,0 +1,2 @@ +dist +package.json diff --git a/packages/ipfs-api/.prettierrc b/packages/ipfs-api/.prettierrc new file mode 100644 index 00000000..963354f2 --- /dev/null +++ b/packages/ipfs-api/.prettierrc @@ -0,0 +1,3 @@ +{ + "printWidth": 120 +} diff --git a/packages/ipfs-api/package.json b/packages/ipfs-api/package.json index c80763a2..230ec5b3 100644 --- a/packages/ipfs-api/package.json +++ b/packages/ipfs-api/package.json @@ -28,6 +28,7 @@ "@types/got": "9.6.12", "@types/mime-types": "2.1.1", "@types/node": "16.9.2", + "prettier": "^2.4.1", "rimraf": "3.0.2", "ts-node": "10.2.1", "tslint": "6.1.3", diff --git a/packages/ipfs-api/src/consts.ts b/packages/ipfs-api/src/consts.ts index 56c9e1c9..c5ce1334 100644 --- a/packages/ipfs-api/src/consts.ts +++ b/packages/ipfs-api/src/consts.ts @@ -2,6 +2,7 @@ export const API_HOSTNAME = "0.0.0.0"; export const API_PORT = "3100"; export const UPLOAD_PATH = "/tmp"; export const IPFS_INFURA_API = "https://ipfs.infura.io:5001"; +export const IPFS_INTERNAL_API = "http://10.10.10.57:5001"; export const IPFS_GATEWAY = "https://cloudflare-ipfs.com/ipfs/"; export const SKYNET_PORTAL = "https://siasky.net"; export const MONGO_CONNECTIONSTRING = `mongodb://${process.env.IPFS_MONGO_USERNAME}:${process.env.IPFS_MONGO_PASSWORD}@ipfs-mongo:27017`; diff --git a/packages/ipfs-api/src/mongodb.ts b/packages/ipfs-api/src/mongodb.ts index c2523c6c..0357650e 100644 --- a/packages/ipfs-api/src/mongodb.ts +++ b/packages/ipfs-api/src/mongodb.ts @@ -1,19 +1,10 @@ -import { - Collection, - CreateIndexesOptions, - Db, - MongoClient, - MongoClientOptions, -} from "mongodb"; +import { Collection, CreateIndexesOptions, Db, MongoClient, MongoClientOptions } from "mongodb"; export class MongoDB { private db: Db; private client: MongoClient; - public constructor( - private connectionString: string, - private databaseName: string - ) {} + public constructor(private connectionString: string, private databaseName: string) {} public async connect() { const options: MongoClientOptions = { @@ -27,9 +18,7 @@ export class MongoDB { this.db = this.client.db(this.databaseName); } - public async createCollection( - collectionName: string - ): Promise> { + public async createCollection(collectionName: string): Promise> { return await this.db.createCollection(collectionName); } @@ -37,12 +26,8 @@ export class MongoDB { await this.db.dropCollection(collectionName); } - public async ensureCollection( - collectionName: string - ): Promise> { - const collections = await this.db - .listCollections({ name: collectionName }) - .toArray(); + public async ensureCollection(collectionName: string): Promise> { + const collections = await this.db.listCollections({ name: collectionName }).toArray(); const collection = collections.length ? (this.db.collection(collectionName) as Collection) @@ -51,19 +36,13 @@ export class MongoDB { return collection; } - public async ensureIndex( - collectionName: string, - fieldOrSpec: any, - options?: CreateIndexesOptions - ): Promise { + public async ensureIndex(collectionName: string, fieldOrSpec: any, options?: CreateIndexesOptions): Promise { const collection = await this.ensureCollection(collectionName); const ensured = await collection.createIndex(fieldOrSpec, options); return ensured; } - public async getCollection( - collectionName: string - ): Promise> { + public async getCollection(collectionName: string): Promise> { return this.ensureCollection(collectionName); } } diff --git a/packages/ipfs-api/src/server.ts b/packages/ipfs-api/src/server.ts index 00f3443b..e11f8b59 100644 --- a/packages/ipfs-api/src/server.ts +++ b/packages/ipfs-api/src/server.ts @@ -1,25 +1,13 @@ import cors from "cors"; import express, { Request, Response } from "express"; import fs from "fs"; +import got from "got"; import { extension as toExtension } from "mime-types"; import { Collection } from "mongodb"; -import { - API_HOSTNAME, - API_PORT, - MONGO_CONNECTIONSTRING, - MONGO_DBNAME, - UPLOAD_PATH, -} from "./consts"; +import { API_HOSTNAME, API_PORT, MONGO_CONNECTIONSTRING, MONGO_DBNAME, UPLOAD_PATH, IPFS_INTERNAL_API } from "./consts"; import { MongoDB } from "./mongodb"; import { IRecord } from "./types"; -import { - contentType, - download, - extractArchive, - isDirectory, - uploadDirectory, - uploadFile, -} from "./utils"; +import { contentType, download, extractArchive, isDirectory, uploadDirectory, uploadFile } from "./utils"; require("dotenv").config(); @@ -46,17 +34,17 @@ require("dotenv").config(); return handleGetLink(req, res, recordsDB); }); + app.get("/ipfs/name/resolve/:name", async (req: Request, res: Response) => { + return await got.post(`${IPFS_INTERNAL_API}/api/v0/name/resolve?arg=${req.params.name}`).json(); + }); + // start the server app.listen(parseInt(API_PORT, 10), API_HOSTNAME, () => { console.log(`IPFS to Skynet API listening at ${API_HOSTNAME}:${API_PORT}`); }); })(); -async function handleGetLink( - req: Request, - res: Response, - recordsDB: Collection -) { +async function handleGetLink(req: Request, res: Response, recordsDB: Collection) { try { const { cid } = req.params; @@ -84,10 +72,7 @@ async function handleGetLink( } } -async function reuploadFile( - cid: string, - recordsDB: Collection -): Promise { +async function reuploadFile(cid: string, recordsDB: Collection): Promise { // get the content type const ct = await contentType(cid); const ext = toExtension(ct); diff --git a/packages/ipfs-api/src/utils.ts b/packages/ipfs-api/src/utils.ts index 880b7b70..bbf64c8a 100644 --- a/packages/ipfs-api/src/utils.ts +++ b/packages/ipfs-api/src/utils.ts @@ -18,14 +18,8 @@ export async function isDirectory(cid: string): Promise { return Boolean(json["Links"].length); } -export async function download( - cid: string, - destination: string, - directory: boolean -): Promise { - const url = directory - ? `${IPFS_INFURA_API}/api/v0/get?arg=${cid}&archive=true` - : `${IPFS_GATEWAY}/${cid}`; +export async function download(cid: string, destination: string, directory: boolean): Promise { + const url = directory ? `${IPFS_INFURA_API}/api/v0/get?arg=${cid}&archive=true` : `${IPFS_GATEWAY}/${cid}`; return new Promise((resolve, reject) => { const downloadStream = got.stream(url); @@ -50,10 +44,7 @@ export async function download( export async function extractArchive(src: string, dst: string) { return new Promise((resolve, reject) => { - createReadStream(src) - .pipe(extract(dst)) - .on("finish", resolve) - .on("error", reject); + createReadStream(src).pipe(extract(dst)).on("finish", resolve).on("error", reject); }); } diff --git a/packages/ipfs-api/yarn.lock b/packages/ipfs-api/yarn.lock index a4602792..e3f9116d 100644 --- a/packages/ipfs-api/yarn.lock +++ b/packages/ipfs-api/yarn.lock @@ -1053,6 +1053,11 @@ post-me@^0.4.5: resolved "https://registry.yarnpkg.com/post-me/-/post-me-0.4.5.tgz#6171b721c7b86230c51cfbe48ddea047ef8831ce" integrity sha512-XgPdktF/2M5jglgVDULr9NUb/QNv3bY3g6RG22iTb5MIMtB07/5FJB5fbVmu5Eaopowc6uZx7K3e7x1shPwnXw== +prettier@^2.4.1: + version "2.4.1" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.4.1.tgz#671e11c89c14a4cfc876ce564106c4a6726c9f5c" + integrity sha512-9fbDAXSBcc6Bs1mZrDYb3XKzDLm4EXXL9sC1LqKP5rZkT6KRr/rf9amVUcODVXgguK/isJz0d0hP72WeaKWsvA== + proper-lockfile@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/proper-lockfile/-/proper-lockfile-2.0.1.tgz#159fb06193d32003f4b3691dd2ec1a634aa80d1d"