🚀 DAY 5 OF LAUNCH WEEK:Introducing Webhook Events for Alert Changes.Learn more →
Socket
Book a DemoInstallSign in
Socket

@bcgov/file-cache

Package Overview
Dependencies
Maintainers
12
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bcgov/file-cache

Simple file storage that generates hashes for stored files

latest
Source
npmnpm
Version
1.0.6
Version published
Maintainers
12
Created
Source

[Deprecated] file-cache

Note: This package has been deprecated and will no longer be receiving support updates.

npm downloads License img

Library to store files locally identified by a hash of the file contents. A sub-directory is created and identified by a hash of the file, the original file is then stored under the hash sub-directory.

The hash is created when writing the binary contents to disk. Each file will generate a unique hash.

/root-dir
.. /hash
.. / ../original-file.ext

Installation

npm i @bcgov/file-cache

Configuration

The root directory for the file cache can be specified by an option (fileCachePath), or an environment variable (FILE_CACHE_PATH), or will just use the temp directory as specified by the Operating System.

Options

The default FILE_CACHE_PATH will use the Operating System's default temporary directory. However, you may override this behavior with the following:

Direct

const FileCache = require('@bcgov/file-cache');
const fileCache = new FileCache({fileCachePath: '/var/usr/file-cache'});

Environment Variables

export FILE_CACHE_PATH = '/var/usr/file-cache';
const FileCache = require('@bcgov/file-cache');
const fileCache = new FileCache();

Usage

create/initialize

Create a new fileCache object, the configured directory will be verified and created if required. Will throw an Error if the directory does not exist and cannot be created.

const FileCache = require('@bcgov/file-cache');
const fileCache = new FileCache({fileCachePath: '/var/usr/file-cache'});

async write

Write contents of a buffer to the file cache.

const writeFileResult = await fileCache.write(input.content, input.fileName, 'binary', {overwrite: true});

ParametersDescription
contentstring or buffer of data
nameFilename or extension or content. If extension, then UUID will be used for the name: UUID.ext
contentEncodingTypeEncoding type of content. ex. base64, bin, hex. Default is base64
optionsObject for optional work. default is {overwrite = false}.
options.overwriteIf true, then allow the destination file to be overwritten if it exists, otherwise will return an error.

Returns

{
    "success": false,
    "errorType": null,
    "errorMsg": null,
    "hash": null
}
FieldDescription
successboolean, true indicates data written to cache, false otherwise
errorTypenumber - error number if not successful.
errorMsgstring - error message if not successful.
hashstring - hash/key identifying the file

async move

Move existing file to file cache.

const moveFileResult = await fileCache.move(req.file.path, req.file.originalname);
ParametersDescription
sourceexisting file
namename of file with extension ex. my-word-file.docx
optionsObject for optional work. default is {overwrite = false}.
options.overwriteIf true, then allow the destination file to be overwritten if it exists, otherwise will return an error.

Returns object

{
    "success": false,
    "errorType": null,
    "errorMsg": null,
    "hash": null
}
FieldDescription
successboolean, true indicates data written to cache, false otherwise
errorTypenumber - error number if not successful.
errorMsgstring - error message if not successful.
hashstring - hash/key identifying the file

async read

Read file from cache. Will throw an Error if file not found or error reading from file system.

const cachedFile = await fileCache.read(hash);
ParametersDescription
hashstring - hash/key for file in cache

Returns file

See fs.readFile

find

Find a file in the cache.

const findFileResult = fileCache.find(hash);
ParametersDescription
hashstring - hash/key for file in cache

Returns object

{
    "success": false,
    "errorType": null,
    "errorMsg": null,
    "hash": null,
    "name": null,
    "ext": null,
    "dir": null,
    "path": null
}
FieldDescription
successboolean, true indicates file found to cache, false otherwise
errorTypenumber - error number if not successful.
errorMsgstring - error message if not successful.
hashstring - hash/key identifying the file
namename original file. ex. my-word-file.docx
extextension portion of file name. ex. .docx
dirdirectory portion of full path (root dir + hash)
pathfull path to original file in cache

remove

Remove a file from the cache.

const removeFileResult = await fileCache.remove(hash);
ParametersDescription
hashstring - hash/key for file in cache

Returns object

{
    "success": false,
    "errorType": null,
    "errorMsg": null
}
FieldDescription
successboolean, true indicates file found and removed from cache, false otherwise
errorTypenumber - error number if not successful.
errorMsgstring - error message if not successful.

Keywords

file

FAQs

Package last updated on 27 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts