Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
ipfs-images-components
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ipfs-images
ipfs-images-components
Commits
69139dda
Commit
69139dda
authored
Apr 28, 2018
by
Дмитрий Никулин
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce a concept of 'storage backend'; add fs backend
parent
03e99b2a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
115 additions
and
45 deletions
+115
-45
filesystem.js
components/storage-backends/filesystem.js
+70
-0
ipfs.js
components/storage-backends/ipfs.js
+45
-45
No files found.
components/storage-backends/filesystem.js
0 → 100644
View file @
69139dda
const
Promise
=
require
(
'bluebird'
);
const
fs
=
require
(
'fs-extra'
);
const
crypto
=
require
(
'crypto'
);
const
path
=
require
(
'path'
);
const
logging
=
require
(
'../logging'
).
getWrapperForModule
(
'filesystem'
);
const
HASH_ALGORITHM
=
'sha256'
;
class
FilesystemStorage
{
/**
* @param {string} basePath - A root directory for all stored files
*/
constructor
(
basePath
)
{
this
.
basePath
=
basePath
;
}
/**
* Uploads attachment to file storage.
* @param {File} file Multipart/form-data file.
* @return {Promise<string>} Hash of uploaded attachment.
*/
add
(
file
)
{
logging
.
verbose
(
`Uploading '
${
file
.
originalFilename
}
' to filesystem`
);
const
hash
=
this
.
computeHash
(
file
);
const
filePath
=
this
.
getFilePath
(
hash
);
return
fs
.
outputFile
(
filePath
,
file
).
then
(()
=>
hash
);
}
/**
* Deletes a file.
*
* @param {string} hash - Hash of file to delete
* @return {Promise<void>}
*/
delete
(
hash
)
{
logging
.
verbose
(
`Deleting file
${
hash
}
from filesystem`
);
const
filePath
=
this
.
getFilePath
(
hash
);
return
fs
.
remove
(
filePath
);
}
/**
* Gets file from IPFS.
* @param {String} hash IPFS hash.
* @return {Promise<ReadableStream>} Readable Stream of attachment.
*/
get
(
hash
)
{
logging
.
verbose
(
`Serving attachment for
${
hash
}
from filesystem`
);
const
filePath
=
this
.
getFilePath
(
hash
);
return
fs
.
readFile
(
filePath
);
}
computeHash
(
file
)
{
const
hash
=
crypto
.
createHash
(
HASH_ALGORITHM
);
hash
.
update
(
file
);
return
hash
.
digest
(
'hex'
);
}
getFilePath
(
hash
)
{
const
dirName
=
hash
.
slice
(
0
,
2
);
const
fileName
=
hash
.
slice
(
2
);
return
path
.
join
(
this
.
basePath
,
dirName
,
fileName
);
}
}
\ No newline at end of file
components/ipfs.js
→
components/
storage-backends/
ipfs.js
View file @
69139dda
'use strict'
;
const
fs
=
require
(
'fs'
);
const
ipfsAPI
=
require
(
'ipfs-api'
);
const
path
=
require
(
'path'
);
const
FileNotFoundError
=
require
(
'./errors'
).
FileNotFoundError
;
const
logging
=
require
(
'./logging'
).
getWrapperForModule
(
'ipfs'
);
const
settings
=
require
(
'./settings'
);
const
FileNotFoundError
=
require
(
'.
.
/errors'
).
FileNotFoundError
;
const
logging
=
require
(
'.
.
/logging'
).
getWrapperForModule
(
'ipfs'
);
const
settings
=
require
(
'.
.
/settings'
);
/**
* Represents an interface to interact with IPFS daemon to download and upload
...
...
@@ -16,67 +14,69 @@ class Ipfs {
/**
* constructor for Ipfs class.
*/
constructor
(
)
{
this
.
ipfs
=
ipfsAPI
(
settings
.
ipfs
.
url
);
constructor
(
url
)
{
this
.
ipfs
=
ipfsAPI
(
url
);
}
/**
*
Stores an IPFS object from a given path locally to disk
.
* @param {
String} hash IPFS hash
.
* @return {Promise
} Pinned objec
t.
*
Uploads attachment to IPFS
.
* @param {
File} file Multipart/form-data file
.
* @return {Promise
<string>} IPFS hash of uploaded attachmen
t.
*/
pin
(
hash
)
{
logging
.
verbose
(
`Pinning object
${
hash
}
to local storage`
);
return
this
.
ipfs
.
pin
.
add
(
hash
);
add
(
file
)
{
logging
.
verbose
(
`Uploading '
${
file
.
originalFilename
}
' to IPFS`
);
let
files
=
[{
path
:
path
.
basename
(
file
.
path
),
content
:
fs
.
createReadStream
(
file
.
path
),
}];
let
hash
;
return
this
.
ipfs
.
files
.
add
(
files
).
then
((
res
)
=>
{
hash
=
res
;
return
this
.
pin
(
hash
);
}).
then
(()
=>
{
return
Promise
.
resolve
(
hash
);
});
}
/**
* Unpins a file from IPFS - it will be deleted during next `ipfs repo gc`
*
* @param {string} hash - Hash of file to delete
* @return {Promise<void>}
*/
delete
(
hash
)
{
logging
.
verbose
(
`Unpinning object
${
hash
}
from local storage`
);
return
this
.
ipfs
.
pin
.
rm
(
hash
);
}
/**
*
Serves attachment to the client
.
*
Gets file from IPFS
.
* @param {String} hash IPFS hash.
* @return {Promise
}
Readable Stream of attachment.
* @return {Promise
<ReadableStream>}
Readable Stream of attachment.
*/
serve
(
hash
)
{
get
(
hash
)
{
logging
.
verbose
(
`Serving attachment for
${
hash
}
`
);
return
this
.
ipfs
.
files
.
cat
(
hash
).
then
((
stream
)
=>
{
if
(
!
(
stream
&&
stream
.
pipe
))
if
(
!
(
stream
&&
stream
.
pipe
))
{
throw
new
FileNotFoundError
(
'No file or stream provided'
);
}
return
stream
;
});
}
/**
* Removes the pin from the given object allowing it to be garbage
* collected if needed.
* @param {String} hash IPFS hash.
* @return {Promise} Unpinned object.
*/
unpin
(
hash
)
{
logging
.
verbose
(
`Unpinning object
${
hash
}
from local storage`
);
return
this
.
ipfs
.
pin
.
rm
(
hash
);
}
/**
*
Uploads attachment to IPFS
.
* @param {
File} file Multipart/form-data file
.
* @return {Promise
} IPFS hash of uploaded attachment.
*
Pins a file by hash
.
* @param {
String} hash IPFS hash
.
* @return {Promise
<void>}
*/
upload
(
file
)
{
logging
.
verbose
(
`Uploading '
${
file
.
originalFilename
}
' to IPFS`
);
let
files
=
[{
path
:
path
.
basename
(
file
.
path
),
content
:
fs
.
createReadStream
(
file
.
path
),
}];
let
hash
;
return
this
.
ipfs
.
files
.
add
(
files
).
then
((
res
)
=>
{
hash
=
res
;
return
this
.
pin
(
hash
);
}).
then
(()
=>
{
return
Promise
.
resolve
(
hash
);
});
pin
(
hash
)
{
logging
.
verbose
(
`Pinning object
${
hash
}
to local storage`
);
return
this
.
ipfs
.
pin
.
add
(
hash
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment