-
-
Notifications
You must be signed in to change notification settings - Fork 169
remove
dr.dimitru edited this page Jul 22, 2018
·
9 revisions
Remove records from FilesCollection and files from FS.
-
selector
{Object} - See Mongo Selectors -
cb
{Function} - Callback, with oneerror
argument - Returns {FilesCollection} - Current FilesCollection instance
import { FilesCollection } from 'meteor/ostrio:files';
const Images = new FilesCollection({collectionName: 'Images'});
// Usage:
// Drop collection's data and remove all associated files from FS
Images.remove({});
// Remove particular file
Images.remove({_id: 'Rfy2HLutYK4XWkwhm'});
// Equals to above
Images.findOne({_id: 'Rfy2HLutYK4XWkwhm'}).remove();
// Direct Collection usage
// Remove record(s) ONLY from collection
Images.collection.remove({});
// Using callback
Images.remove({_id: 'Rfy2HLutYK4XWkwhm'}, (error) => {
if (error) {
console.error(`File wasn't removed, error: ${error.reason}`);
} else {
console.info('File successfully removed');
}
});
Use onBeforeRemove to avoid unauthorized actions, for more info see onBeforeRemove callback
import { FilesCollection } from 'meteor/ostrio:files';
const Images = new FilesCollection({
collectionName: 'Images',
allowClientCode: true,
onBeforeRemove(cursor) {
const records = cursor.fetch();
if (records && records.length) {
if (this.userId) {
const user = this.user();
// Assuming user.profile.docs is array
// with file's records _id(s)
for (let i = 0, len = records.length; i < len; i++) {
const file = records[i];
if (!~user.profile.docs.indexOf(file._id)) {
// Return false if at least one document
// is not owned by current user
return false;
}
}
}
}
return true;
}
});
Meteor-Files | Support | Try ostr.io |
---|---|---|
If you found this package useful, — star it at GitHub and support our open source contributions with a monthly pledge, or submit a one-time donation via PayPal | Monitoring, Analytics, WebSec, Web-CRON and Pre-rendering for a website |