This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af19f13
commit 17b3456
Showing
7 changed files
with
2,402 additions
and
3,194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import axios, { AxiosInstance } from 'axios'; | ||
import FormData from 'form-data'; | ||
import { Readable } from 'stream'; | ||
import { Logger, log } from '../../../src/lib/Logger'; | ||
|
||
export default class IpfsServerClient { | ||
private instance: AxiosInstance; | ||
|
||
constructor({ | ||
baseURL, | ||
projectId, | ||
apiKeySecret, | ||
}: { | ||
baseURL: string; | ||
projectId: string; | ||
apiKeySecret: string; | ||
}) { | ||
if (!baseURL) | ||
log.throwMissingArgumentError(Logger.message.no_base_url, { | ||
location: Logger.location.IPFS_SERVER_CONSTRUCTOR, | ||
}); | ||
|
||
if (!projectId) | ||
log.throwMissingArgumentError(Logger.message.no_projectId_supplied, { | ||
location: Logger.location.IPFS_SERVER_CONSTRUCTOR, | ||
}); | ||
|
||
if (!apiKeySecret) | ||
log.throwMissingArgumentError(Logger.message.no_secretId_supplied, { | ||
location: Logger.location.IPFS_SERVER_CONSTRUCTOR, | ||
}); | ||
|
||
this.instance = axios.create({ | ||
baseURL, | ||
headers: { | ||
authorization: `Basic ${Buffer.from(`${projectId}:${apiKeySecret}`).toString('base64')}`, | ||
}, | ||
}); | ||
} | ||
|
||
async add(content: string | Readable): Promise<string> { | ||
const formData = new FormData(); | ||
formData.append('file', content); | ||
|
||
try { | ||
const response = await this.instance.post('/add', formData, { | ||
headers: { | ||
'Content-Type': `multipart/form-data; boundary=${formData.getBoundary()}`, | ||
}, | ||
}); | ||
return response.data.Hash; | ||
} catch (error) { | ||
return log.throwArgumentError( | ||
Logger.message.an_error_occured_with_ipfs_api, | ||
'file', | ||
content, | ||
{ | ||
location: Logger.location.IPFSSERVICE_UPLOADFILE, | ||
error: `${error}`, | ||
}, | ||
); | ||
} | ||
} | ||
|
||
async addAll(files: { path: string; content: any }[]): Promise<any> { | ||
const formData = new FormData(); | ||
|
||
files.forEach(file => { | ||
formData.append(file.path, file.content, { | ||
filename: file.path, | ||
}); | ||
}); | ||
|
||
try { | ||
const response = await this.instance.post(`/add?wrap-with-directory=true`, formData, { | ||
headers: { | ||
'Content-Type': `multipart/form-data; boundary=${formData.getBoundary()}`, | ||
}, | ||
}); | ||
|
||
const arr = response.data.split('\n').filter(Boolean).map(JSON.parse); | ||
const lastItem = arr.pop(); | ||
return lastItem.Hash; | ||
} catch (error) { | ||
return log.throwArgumentError(Logger.message.an_error_occured_with_ipfs_api, 'file', files, { | ||
location: Logger.location.IPFSSERVICE_UPLOADFILE, | ||
error: `${error}`, | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.