file-force

Форк
0
/
libipfs.js 
94 строки · 2.5 Кб
1
/*
2
 *   Copyright (C) 2017 Igor Konovalov
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *   http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16

17
'use strict';
18
const fs = require('fs');
19
const path = require('path');
20
const getFileName = path.basename;
21
const ipfs = require('ipfs-api');
22
const stream = require('stream');
23

24
function isStream (stream) {
25
    return typeof stream === 'object' && typeof stream.pipe === 'function';
26
};
27

28
module.exports = function IPFS(config) {
29

30
    this.instance = ipfs(config.ipfs.api);
31

32
    this.files = this.instance.files;
33

34
    this.addBuffer = (bufferData) => {
35
        const bufferStream = new stream.PassThrough();
36
        bufferStream.end(bufferData);
37
        return this.add(bufferStream);
38
    };
39

40
    this.add = (file) => {
41
        let files;
42
        if (isStream(file)) { // file is stream already
43
            files = [
44
                {
45
                    path: file.constructor.name,
46
                    content: file
47
                }
48
            ];
49
        } else { // prepare stream from file path
50
            const fileStream = fs.createReadStream(file);
51
            const fileName = getFileName(file);
52
            files = [
53
                {
54
                    path: fileName,
55
                    content: fileStream
56
                }
57
            ];
58
        }
59
        return this.instance.add(files);
60
    };
61

62
    /**
63
     * Expose ipfs.get
64
     * @param hash
65
     * @param callback
66
     */
67
    this.get =  (hash, callback) => {
68
        this.files.get(hash, callback)
69
    };
70

71
    this.cat = (hash, callback) => {
72
        this.files.cat(hash, callback)
73
    };
74

75
    /**
76
     * Store requested hash to file path.
77
     * @param hash
78
     * @param localPath
79
     * @param cb
80
     */
81
    this.getAndStore = (hash, localPath, cb) => {
82
        this.get(hash, (error, stream) => {
83
            let destination = fs.createWriteStream(localPath);
84
            stream.on('data', data => {
85
                data.content.pipe(destination);
86
            });
87
            stream.on('end', () => {
88
                if (cb) {
89
                    cb(destination);
90
                }
91
            });
92
        });
93
    }
94
};

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.