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
bfd8bd94
Commit
bfd8bd94
authored
May 03, 2018
by
Дмитрий Никулин
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Some fixes after refactoring
parent
c1f0080f
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
33 additions
and
23 deletions
+33
-23
cleaner.js
components/cleaner.js
+1
-1
download.js
components/handlers/download.js
+2
-1
download_thumb.js
components/handlers/download_thumb.js
+1
-1
upload.js
components/handlers/upload.js
+1
-6
filesystem.js
components/storage-backends/filesystem.js
+11
-8
ipfs.js
components/storage-backends/ipfs.js
+2
-1
index.js
index.js
+13
-3
package.json
package.json
+2
-1
ipfs.js
test/ipfs.js
+0
-1
No files found.
components/cleaner.js
View file @
bfd8bd94
...
...
@@ -5,7 +5,7 @@ const Promise = require('bluebird');
const
logging
=
require
(
'./logging'
).
getWrapperForModule
(
'cleaner'
);
const
Metadata
=
require
(
'./metadata'
);
const
redis
=
require
(
'./redis'
);
const
Ipfs
=
require
(
'./ipfs'
);
const
Ipfs
=
require
(
'./
storage-backends/
ipfs'
);
/**
* Represents a cleaner for attachments.
...
...
components/handlers/download.js
View file @
bfd8bd94
...
...
@@ -3,7 +3,7 @@
const
Promise
=
require
(
'bluebird'
);
const
{
FileNotFoundError
}
=
require
(
'../errors'
);
const
logging
=
requ
est
(
'../logging'
).
getWrapperForModule
(
'download'
);
const
logging
=
requ
ire
(
'../logging'
).
getWrapperForModule
(
'download'
);
/**
* Creates route handler for /<base-url>/:hash/:filename.
...
...
@@ -40,6 +40,7 @@ module.exports = function (storage, metadataStorage, options) {
};
let
handleError
=
(
error
)
=>
{
console
.
log
(
error
);
if
(
error
instanceof
FileNotFoundError
)
{
response
.
sendStatus
(
404
);
}
else
{
...
...
components/handlers/download_thumb.js
View file @
bfd8bd94
...
...
@@ -2,7 +2,7 @@
const
Promise
=
require
(
'bluebird'
);
const
FileNotFoundError
=
require
(
'../errors'
).
FileNotFoundError
;
const
{
FileNotFoundError
}
=
require
(
'../errors'
)
;
const
logging
=
require
(
'../logging'
).
getWrapperForModule
(
'download_thumb'
);
/**
...
...
components/handlers/upload.js
View file @
bfd8bd94
...
...
@@ -48,10 +48,7 @@ module.exports = function (storage, metadataStorage, options) {
return
storage
.
add
(
file
);
};
let
addMetadata
=
(
result
)
=>
{
result
=
result
[
0
];
hash
=
result
.
hash
;
let
addMetadata
=
(
hash
)
=>
{
return
metadataStorage
.
addRecord
(
hash
,
mimetype
,
size
);
};
...
...
@@ -81,5 +78,3 @@ module.exports = function (storage, metadataStorage, options) {
.
catch
((
error
)
=>
logging
.
error
(
error
));
};
};
module
.
exports
=
upload
;
components/storage-backends/filesystem.js
View file @
bfd8bd94
...
...
@@ -23,10 +23,12 @@ class FilesystemStorage {
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
);
return
fs
.
readFile
(
file
.
path
).
then
(
content
=>
{
const
hash
=
this
.
computeHash
(
content
);
const
filePath
=
this
.
getFilePath
(
hash
);
return
fs
.
outputFile
(
filePath
,
content
).
then
(()
=>
hash
);
});
}
/**
...
...
@@ -45,13 +47,13 @@ class FilesystemStorage {
/**
* Gets file from IPFS.
* @param {String} hash IPFS hash.
* @return {
Promise<ReadableStream>
} Readable Stream of attachment.
* @return {
ReadableStream
} Readable Stream of attachment.
*/
get
(
hash
)
{
logging
.
verbose
(
`Serving attachment for
${
hash
}
from filesystem`
);
const
filePath
=
this
.
getFilePath
(
hash
);
return
fs
.
readFile
(
filePath
);
return
fs
.
createReadStream
(
filePath
);
}
...
...
@@ -66,4 +68,6 @@ class FilesystemStorage {
const
fileName
=
hash
.
slice
(
2
);
return
path
.
join
(
this
.
basePath
,
dirName
,
fileName
);
}
}
\ No newline at end of file
}
module
.
exports
=
FilesystemStorage
;
components/storage-backends/ipfs.js
View file @
bfd8bd94
...
...
@@ -32,7 +32,8 @@ class IpfsStorage {
let
hash
;
return
this
.
ipfs
.
files
.
add
(
files
).
then
((
res
)
=>
{
hash
=
res
;
hash
=
res
[
0
].
hash
;
return
this
.
pin
(
hash
);
}).
then
(()
=>
{
return
Promise
.
resolve
(
hash
);
...
...
index.js
View file @
bfd8bd94
...
...
@@ -2,10 +2,9 @@
const
path
=
require
(
'path'
);
le
t
mods
=
[
cons
t
mods
=
[
'cleaner'
,
'errors'
,
'ipfs'
,
'logging'
,
'metadata'
,
'pin'
,
...
...
@@ -14,7 +13,18 @@ let mods = [
'thumbnail'
,
];
module
.
exports
=
{};
module
.
exports
=
{
handlers
:
{
delAttachment
:
require
(
'./components/handlers/del_attachment'
),
download
:
require
(
'./components/handlers/download'
),
downloadThumb
:
require
(
'./components/handlers/download_thumb'
),
upload
:
require
(
'./components/handlers/upload'
),
},
storageBackends
:
{
FilesystemStorage
:
require
(
'./components/storage-backends/filesystem'
),
IpfsStorage
:
require
(
'./components/storage-backends/ipfs'
),
},
};
mods
.
forEach
((
mod
)
=>
{
module
.
exports
[
mod
]
=
require
(
...
...
package.json
View file @
bfd8bd94
...
...
@@ -9,8 +9,9 @@
"private"
:
true
,
"dependencies"
:
{
"
bluebird
"
:
"3.5.0"
,
"
fs-extra
"
:
"
2.1.2
"
,
"
fs-extra
"
:
"
^6.0.0
"
,
"
ipfs-api
"
:
"14.0.0"
,
"
multiparty
"
:
"^4.1.3"
,
"
redis
"
:
"2.7.1"
,
"
sharp
"
:
"0.17.3"
,
"
winston
"
:
"2.3.1"
...
...
test/ipfs.js
View file @
bfd8bd94
...
...
@@ -42,7 +42,6 @@ describe('Ipfs', () => {
it
(
'should return hash'
,
function
(
done
)
{
this
.
timeout
(
30000
);
ipfs
.
add
(
file
).
then
((
hash
)
=>
{
hash
=
hash
[
0
].
hash
;
hash
.
should
.
startWith
(
'Qm'
);
hash
.
should
.
have
.
length
(
46
);
done
();
...
...
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