attachments.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import Attachments, { AttachmentStorage } from '/models/attachments';
  2. import { ObjectID } from 'bson';
  3. Meteor.publish('attachmentsList', function() {
  4. // eslint-disable-next-line no-console
  5. // console.log('attachments:', AttachmentStorage.find());
  6. const files = AttachmentStorage.find(
  7. {},
  8. {
  9. fields: {
  10. _id: 1,
  11. filename: 1,
  12. md5: 1,
  13. length: 1,
  14. contentType: 1,
  15. metadata: 1,
  16. },
  17. sort: {
  18. filename: 1,
  19. },
  20. limit: 250,
  21. },
  22. );
  23. const attIds = [];
  24. files.forEach(file => {
  25. attIds.push(file._id._str);
  26. });
  27. return [
  28. files,
  29. Attachments.find({ 'copies.attachments.key': { $in: attIds } }),
  30. ];
  31. });
  32. Meteor.publish('orphanedAttachments', function() {
  33. let keys = [];
  34. Attachments.find({}, { fields: { copies: 1 } }).forEach(att => {
  35. keys.push(new ObjectID(att.copies.attachments.key));
  36. });
  37. keys.sort();
  38. keys = _.uniq(keys, true);
  39. return AttachmentStorage.find(
  40. { _id: { $nin: keys } },
  41. {
  42. fields: {
  43. _id: 1,
  44. filename: 1,
  45. md5: 1,
  46. length: 1,
  47. contentType: 1,
  48. metadata: 1,
  49. },
  50. sort: {
  51. filename: 1,
  52. },
  53. limit: 250,
  54. },
  55. );
  56. });