Astronome v0.3.0
Meteor server-side package for populating Meteor.Collections
with informations on existing system directories and files, without touching (almost) anything, and without preventing normal interaction with those files and directories through filesystem.
Usecases: indexing and adding metadatas to personnal files (pictures, videos, whatnot...) and still being able to manipulate them through filesystem.
How it works
- Manage records in a user-provided
Meteor.Collections
for subdirectories (recursively) and for files. - Callbacks on various events to allow better control of parsing.
- Hidden file into each tracked subdirectory with its id, so that tracking isn't lost just because directory has been renamed or moved.
- Files are matched by name: tracking will be lost if the file is renamed or moved to a different directory.
Example
Directories = new Meteor.Collection("Directories");
Files = new Meteor.Collection("Files");
if (Meteor.isServer) {
Meteor.startup(function () {
// Create and check parameters
var p = Astronome.checkParams({
'sourcePath' : "/mypictures/",
'idFilename' : '.astronomeid'
'directoryCollection' : Directories,
'fileCollection' : Files,
'onDirectoryAddedBeforeCB' : function(directoryfullpath){ return true; }
'onDirectoryAddedAfterCB' : function(directory){ Directories.update(directory, {some:metadatas});},
'onDirectoryDeletedCB' : function(directory){ return true; },
'onDirectoryMovedCB' : function(directory, olddirectorypath){ },
'onFileAddedBeforeCB' : function(filefullpath, basename, extension){return true},
'onFileAddedAfterCB' : function(file){ },
'onFileDeletedCB' : function(file){ console.log(this.someUserData); },
'onFileChangedCB' : function(file){ },
'someUserData' : 42,
});
// Parse directory every minute
Meteor.setInterval(function(){
Astronome.parse(p);
}, 60000);
}
}
API
Errors
Astronome
uses Meteor.Error
to throw detailed errors
Parameters
The parameters object mainly tells Astronome
what collection to feed and what callbacks to call.
The fields described below are the one that will be read by Astronome
.
required
sourcePath Must be a string containing the disk path of the existing 'source' directory (that is to be recursively tracked).
required
directoryCollection Must be a valid Meteor.Collection to store information on Directories
required
fileCollection Must be a valid Meteor.Collection to store informations on Files
default is ".astronomeid"
idFilename Must be a string containing the name of the file that will be created in each subdirectory and contain the id of this subdirectory in the directoryCollection
optional
onDirectoryAddedBeforeCB(fulldirectorypath) - Called by the
parse
function for each directory that is not tracked indirectoryCollection
. - Return value controls whether the subdirectory should be tracked (
true
) or not (false
). - If this callback is not defined, all directories will be tracked.
- Not called for the source directory (that is never tracked)
optional
onDirectoryAddedAfterCB(directory) - If defined, called by the
parse
function just after thedirectory
object has been inserted to thedirectoryCollection
. - Return value controls whether this directory should be kept (
true
) or removed (false
) in database.
optional
onDirectoryDeletedCB(directory) - Called by the
parse
function for each trackeddirectory
that is missing on the filesystem - Return value controls whether the
directory
is removed from database (true
) or keeped with a null source (false
). - If this callback is not defined, all missing directories will be removed from database.
optional
onDirectoryMovedCB(directory, oldDirectoryPath) - If defined, called by the
parse
function after a trackeddirectory
that has been moved fromoldDirectoryPath
has been updated. - Return value controls whether this directory should be kept (
true
) or removed (false
) in database.
optional
onDirectoryForgottenCB(directory) - If defined, called by the
forget
function just before a trackeddirectory
is forgot. - Return value isn't used.
optional
onFileAddedBeforeCB(filename, basename, extension) - Called by the
parse
function for each file that has no record infileCollection
. - File is named
filename
(basename
.extension
) and its parent directory informations can be found inthis.astr.parentDir
. - File's
basename
andextension
are also provided and will be stored infileCollection
. - Return value controls whether a record should be inserted in
fileCollection
for this file (true
) or not (false
). - If this callback is not defined, all files will be tracked.
- No record is called for tracker files named
params.idFilename
.
optional
onFileAddedAfterCB(file) - If defined, called by the
parse
function just after thefile
object has been inserted to thefileCollection
. - Return value controls whether this file should be kept (
true
) or removed (false
) in database.
optional
onFileDeletedCB(file) - Called by the
parse
function for each trackedfile
that is missing on the filesystem - Return value controls whether the
file
is removed from database (true
) or keeped with a null source (false
). - If this callback is not defined, all missing files will be removed from database.
optional
onFileChangedCB() - If defined, called by the
parse
function for eachfile
whose modification time is newer than the last parse time - Return value controls whether this file should be kept (
true
) or removed (false
) in database.
optional
onFileForgottenCB(file) - If defined, called by the
forget
function just before a trackedfile
is forgot. - Return value isn't used.
Additional properties
Additionnal properties can be added to the parameters object, at user's discretion.
Those extra properties will be accessible in read/write in any of the callbacks through this
.
Those properties are depth-linked. For example if you store the directory path in the onDirectoryAddedBeforeCB
, and you read it in onFileAddedBeforeCB
you will always get the correct parent's directory path, even if there were some nested subfolders that have been added between the two calls.
This is done by backuping recursively the extra own properties each time we parse deeper:
- Extra properties should ideally be as flat and light as possible (for instance, not a meteor Collection !, which is big and recursive), otherwise it will be slow or not work at all (let me know).
- If the param object inherits from something, the inherited properties won't get backuped. This can be exploited to store global user data that must not be depth-linked.
parse(params)
Recursively parse the folders of a given source
, and update both fileCollection
and directoryCollection
.
Call callbacks defined in params
for various usecases.
forget(params)
Recursively remove all tracker files in the folders of a given source
, and remove every record corresponding to elements of this source in both fileCollection
and directoryCollection
.
Call callbacks defined in params
for various usecases.
checkParams(params)
Will check that provided params are valid, and create default values for missing optionnal parameters.
TODO
- Remove
recursive-fs
dependency that is only used in tests - Add depth control
maxDepth
to recursive parsing - Add function to use filesystem events instead of periodical reparsing
Changelog
- 0.3.0
- Better handling of cases were tracked folder are missing from database (or exist in databases and are untracked on disk)
-
onDirectoryAddedAfterCB
onDirectoryMovedCB
must return true upon completion, otherwise the concerned directory will get untracked. -
onFileChangedCB
onFileAddedAfterCB
must return true upon completion, otherwise the concerned file will get untracked. - Add
basename
andextension
to the File collection and as argument to theonFileAddedBeforeCB
callback
- 0.2.0
- Use additional params properties instead of userdata extra arg to all callbacks.
- 0.1.0
- Update to official meteor packaging system