attachments.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. if (Attachments.find({}, { fields: { copies: 1 } }) !== undefined) {
  35. Attachments.find({}, { fields: { copies: 1 } }).forEach(att => {
  36. keys.push(new ObjectID(att.copies.attachments.key));
  37. });
  38. keys.sort();
  39. keys = _.uniq(keys, true);
  40. return AttachmentStorage.find(
  41. { _id: { $nin: keys } },
  42. {
  43. fields: {
  44. _id: 1,
  45. filename: 1,
  46. md5: 1,
  47. length: 1,
  48. contentType: 1,
  49. metadata: 1,
  50. },
  51. sort: {
  52. filename: 1,
  53. },
  54. limit: 250,
  55. },
  56. );
  57. } else {
  58. return [];
  59. }
  60. });