files-repo
Advanced tools
Comparing version 1.3.1 to 2.0.0
46
index.js
@@ -43,9 +43,10 @@ "use strict"; | ||
let ext = Path.extname(pathToFile); | ||
if ( typeof fileId == 'undefined' ) { | ||
fileId = Utils.newUUID(); | ||
} | ||
let loc = this.Helper.locationFromFileId(fileId, this.config.Size); | ||
let fileContent = await Utils.readFile( pathToFile ); | ||
let fileFolder = Path.join( this.config.Path, this.Helper.locationFromFileId(fileId, this.config.Size) ); | ||
let fileFolder = Path.join( this.config.Path, loc ); | ||
let fullPathToFileInRepo = Path.join( fileFolder, fileId ); | ||
@@ -61,3 +62,3 @@ | ||
created: new Date(), | ||
location: fullPathToFileInRepo + ext | ||
localPath: `/${loc}/${fileId}.${ext}` | ||
} | ||
@@ -70,6 +71,15 @@ | ||
async UpdateFileManifest( fileId, manifest ) { | ||
let loc = this.Helper.locationFromFileId(fileId, this.config.Size); | ||
let fullPathToFileInRepo = Path.join( this.config.Path, loc, fileId ); | ||
await Utils.saveFile( fullPathToFileInRepo+".manifest", JSON.stringify(manifest) ); | ||
} | ||
async AllocateNewFileLocation( fileExtension ) { | ||
let fileId = Utils.newUUID(); | ||
let fileFolder = Path.join( this.config.Path, this.Helper.locationFromFileId(fileId, this.config.Size) ); | ||
let fullPathToFileInRepo = Path.join( fileFolder, fileId+"."+(fileExtension ? fileExtension : "bin") ); | ||
let loc = this.Helper.locationFromFileId(fileId, this.config.Size); | ||
let fileFolder = Path.join( this.config.Path, loc ); | ||
let ext = fileExtension ? fileExtension : "bin"; | ||
let fullPathToFileInRepo = Path.join( fileFolder, fileId + "." + ext ); | ||
let fullPathToManifest = Path.join( fileFolder, fileId+".manifest" ); | ||
@@ -84,3 +94,3 @@ | ||
created: new Date(), | ||
location: fullPathToFileInRepo | ||
localPath: `/${loc}/${fileId}.${ext}` | ||
} | ||
@@ -95,3 +105,4 @@ | ||
let fileId = Utils.newUUID(); | ||
let fileFolder = Path.join( this.config.Path, this.Helper.locationFromFileId(fileId, this.config.Size) ); | ||
let loc = this.Helper.locationFromFileId(fileId, this.config.Size); | ||
let fileFolder = Path.join( this.config.Path, loc ); | ||
let fullPathToFileInRepo = Path.join( fileFolder, fileId ); | ||
@@ -109,3 +120,3 @@ | ||
created: new Date(), | ||
location: fullPathToFileInRepo + "." + ext | ||
localPath: `/${loc}/${fileId}.${ext}` | ||
} | ||
@@ -181,5 +192,20 @@ | ||
return JSON.parse(await Utils.readFile( fullPathToFileInRepo+".manifest") ); | ||
let manifest = JSON.parse(await Utils.readFile( fullPathToFileInRepo+".manifest") ); | ||
manifest.location = Path.join(this.config.Path, manifest.localPath); | ||
return manifest; | ||
} | ||
/* | ||
* Adds field "metadata" to a file manifest | ||
* with specific information about that file for some client purpose | ||
*/ | ||
async AddMetadataToFile( fileId, metadata ) { | ||
let manifest = await this.GetFileManifest( fileId ); | ||
manifest.metadata = metadata; | ||
await this.UpdateFileManifest( fileId, manifest ); | ||
} | ||
async IterateAll( fnc ) { | ||
@@ -186,0 +212,0 @@ let directories = await Utils.readDirectories(this.config.Path); |
{ | ||
"name": "files-repo", | ||
"version": "1.3.1", | ||
"version": "2.0.0", | ||
"description": "Files manager module for easy files managemente in a NodeJS project", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -210,2 +210,23 @@ "use strict"; | ||
}); | ||
it( '# Add string metadata to a file', async() => { | ||
let metadata = "samplemetadata"; | ||
let fileId1 = await f.AddFromBuffer( Buffer.from("a") ); | ||
await f.AddMetadataToFile( fileId1, metadata ); | ||
let manifest = await f.GetFileManifest( fileId1 ); | ||
Assert.equal( metadata, manifest.metadata ); | ||
}); | ||
it( '# Add object metadata to a file', async() => { | ||
let metadata = { data1: "a", data2: "b"}; | ||
let fileId1 = await f.AddFromBuffer( Buffer.from("a") ); | ||
await f.AddMetadataToFile( fileId1, metadata ); | ||
let manifest = await f.GetFileManifest( fileId1 ); | ||
Assert.equal( metadata.data1, manifest.metadata.data1 ); | ||
Assert.equal( metadata.data2, manifest.metadata.data2 ); | ||
}); | ||
}); |
31012
590