11'use strict'
22
3- const fs = require ( 'fs-extra' )
43const { LdpFileResource, LdpFileContainer } = require ( './ldp-file-resource' )
54
65class LdpFileStore {
76 /**
87 * @param options {object}
98 *
10- * @param options.root {string} Root fs path for data storage
11- * @param options.multiuser {boolean}
9+ * @param options.fs {object} Expects the `fs-extra` API
10+ * @param options.mapper {LegacyResourceMapper}
11+ *
1212 * @param options.suffixAcl {string}
1313 * @param options.suffixMeta {string}
14- * @param options.mapper {LegacyResourceMapper}
1514 * @param options.dataBrowserPath {string}
16- * @param options.suppressDataBrowser
15+ * @param options.suppressDataBrowser {boolean}
1716 */
1817 constructor ( options ) {
19- this . root = options . root
20- this . multiuser = options . multiuser
21- this . suffixAcl = options . suffixAcl
22- this . suffixMeta = options . suffixMeta
23-
18+ this . fs = options . fs
2419 this . mapper = options . mapper
2520
21+ this . suffixAcl = options . suffixAcl
22+ this . suffixMeta = options . suffixMeta
2623 this . dataBrowserPath = options . dataBrowserPath
2724 this . suppressDataBrowser = options . suppressDataBrowser
2825 }
@@ -34,7 +31,7 @@ class LdpFileStore {
3431 * @throws {Error } When encountering a filesystem error that's not "File does
3532 * not exist", such as `EACCES` etc.
3633 *
37- * @see https://nodejs.org/api/fs.html#fs_class_fs_stats
34+ * @see https://nodejs.org/api/this. fs.html#fs_class_fs_stats
3835 *
3936 * @returns {Promise<LdpFileResource|LdpFileContainer> }
4037 */
@@ -46,7 +43,7 @@ class LdpFileStore {
4643
4744 // Try and load file metadata
4845 try {
49- fsStats = await fs . stat ( path )
46+ fsStats = await this . fs . stat ( path )
5047 } catch ( error ) {
5148 if ( error . code === 'ENOENT' ) {
5249 exists = false
@@ -55,12 +52,7 @@ class LdpFileStore {
5552 }
5653 }
5754
58- let isContainer
59- if ( exists ) {
60- isContainer = fsStats . isDirectory ( )
61- } else {
62- isContainer = target . url . endsWith ( '/' )
63- }
55+ const isContainer = fsStats ? fsStats . isDirectory ( ) : target . url . endsWith ( '/' )
6456
6557 const encoding = target . charset ( ) // todo: add default charset 'utf8'?
6658
@@ -77,7 +69,7 @@ class LdpFileStore {
7769 async exists ( target ) {
7870 const resource = await this . resource ( target )
7971
80- return fs . pathExists ( resource . filePath )
72+ return this . fs . pathExists ( resource . filePath )
8173 }
8274
8375 /**
@@ -107,7 +99,7 @@ class LdpFileStore {
10799 * @returns {Promise }
108100 */
109101 async loadContainerContents ( container ) {
110- container . contents = await fs . readdir ( container . path , container . encoding )
102+ container . contents = await this . fs . readdir ( container . path , container . encoding )
111103 }
112104
113105 /**
@@ -116,7 +108,7 @@ class LdpFileStore {
116108 * @returns {Promise }
117109 */
118110 async deleteResource ( resource ) {
119- return fs . remove ( resource . path )
111+ return this . fs . remove ( resource . path )
120112 }
121113
122114 /**
@@ -130,7 +122,7 @@ class LdpFileStore {
130122 * @returns {Promise }
131123 */
132124 async deleteContainer ( container ) {
133- return fs . remove ( container . path )
125+ return this . fs . remove ( container . path )
134126 }
135127}
136128
0 commit comments