-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
115 lines (102 loc) · 2.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"use strict";
/*
* IPFS storage adapter for Ghost.
*
* @author : Alex Baker <alex.baker.fon@gmail.com>
* @updated : 22th Jan 2022
*/
require("dotenv").config();
const StorageBase = require("ghost-storage-base");
const fs = require("fs");
const ipfs = require("ipfs-storage");
class IPFSAdapter extends StorageBase {
constructor(options) {
super(options);
const { defaultStorage } = options;
this.defaultStorage = defaultStorage || "";
this.config = options[defaultStorage] || {};
if (!Object.keys(this.config).length) {
if (this.defaultStorage === "filebase") {
this.config.key = process.env.FILEBASE_KEY;
this.config.secret = process.env.FILEBASE_SECRET;
this.config.bucket = process.env.FILEBASE_BUCKET;
} else if (this.defaultStorage === "pinata") {
this.config.jwt = process.env.PINATA_JWT;
} else if (this.defaultStorage === "fleek") {
this.config.key = process.env.FLEEK_KEY;
this.config.secret = process.env.FLEEK_SECRET;
this.config.bucket = process.env.FLEEK_BUCKET;
} else if (this.defaultStorage === "web3") {
this.config.token = process.env.WEB3_TOKEN;
} else if (this.defaultStorage === "lighthouse") {
this.config.token = process.env.LIGHTHOUSE_TOKEN;
}
}
}
/**
* Checks for existance of file (handle 404's proper).
*
* @param fileName
* @param targetDir
* @returns Promise<boolean>
*/
exists(fileName, targetDir) {
return Promise.resolve(true);
}
/**
* Saves the image to IPFS storage (Filebase, Pinata, Fleek, Web3, etc).
*
* @param image - is the express image object
* @returns Promise.<string> - a promise which ultimately returns the
* full url to the uploaded image
*/
save(image) {
return new Promise(async (resolve, reject) => {
try {
fs.readFile(image.path, async (err, data) => {
if (err) {
return reject(err);
}
const url = await ipfs.uploadFile[this.defaultStorage](this.config, {
hash: image.name,
ext: "",
buffer: data,
});
resolve(url);
});
} catch (e) {
reject(e);
}
});
}
/**
* Ghost calls `.serve()` as part of its middleware stack,
* and mounts the returned function as the middleware for serving images.
*
* @returns Handler
*/
serve() {
return (req, res, next) => {
next();
};
}
/**
* @param fileName
* @returns Promise<boolean>
*/
delete(fileName) {
return new Promise(async (resolve, reject) => {
try {
await ipfs.deleteFile[this.defaultStorage](this.config, fileName);
resolve(true);
} catch (e) {
reject(e);
}
});
}
/**
* @returns Promise<Buffer>
*/
read() {}
}
module.exports = IPFSAdapter;