Ver Fonte

Card clone OK

Romulus Urakagi Tsai há 5 anos atrás
pai
commit
4c5a2fbd1f
3 ficheiros alterados com 42 adições e 8 exclusões
  1. 33 2
      models/attachments.js
  2. 8 4
      models/cards.js
  3. 1 2
      server/migrations.js

+ 33 - 2
models/attachments.js

@@ -1,4 +1,5 @@
 import { FilesCollection } from 'meteor/ostrio:files';
 import { FilesCollection } from 'meteor/ostrio:files';
+const fs = require('fs');
 
 
 const collectionName = 'attachments2';
 const collectionName = 'attachments2';
 
 
@@ -19,6 +20,36 @@ if (Meteor.isServer) {
   // TODO: Permission related
   // TODO: Permission related
   // TODO: Add Activity update
   // TODO: Add Activity update
 
 
+  Meteor.methods({
+    cloneAttachment(file, overrides) {
+      check(file, Object);
+      check(overrides, Match.Maybe(Object));
+      const path = file.path;
+      const opts = {
+          fileName: file.name,
+          type: file.type,
+          meta: file.meta,
+          userId: file.userId
+      };
+      for (let key in overrides) {
+        if (key === 'meta') {
+          for (let metaKey in overrides.meta) {
+            opts.meta[metaKey] = overrides.meta[metaKey];
+          }
+        } else {
+          opts[key] = overrides[key];
+        }
+      }
+      const buffer = fs.readFileSync(path);
+      Attachments.write(buffer, opts, (err, fileRef) => {
+        if (err) {
+          console.log('Error when cloning record', err);
+        }
+      });
+      return true;
+    }
+  });
+
   Meteor.publish(collectionName, function() {
   Meteor.publish(collectionName, function() {
     return Attachments.find().cursor;
     return Attachments.find().cursor;
   });
   });
@@ -51,13 +82,13 @@ function onAttachmentUploaded(fileRef) {
   } else {
   } else {
     // Don't add activity about adding the attachment as the activity
     // Don't add activity about adding the attachment as the activity
     // be imported and delete source field
     // be imported and delete source field
-    CFSAttachments.update(
+    Attachments.collection.update(
       {
       {
         _id: fileRef._id,
         _id: fileRef._id,
       },
       },
       {
       {
         $unset: {
         $unset: {
-          source: '',
+          'meta.source': '',
         },
         },
       },
       },
     );
     );

+ 8 - 4
models/cards.js

@@ -402,10 +402,14 @@ Cards.helpers({
     const _id = Cards.insert(this);
     const _id = Cards.insert(this);
 
 
     // Copy attachments
     // Copy attachments
-    oldCard.attachments().forEach(att => {
-      att.meta.cardId = _id;
-      delete att._id;
-      return Attachments.insert(att);
+    oldCard.attachments().forEach((file) => {
+      Meteor.call('cloneAttachment', file, 
+        {
+          meta: {
+            cardId: _id
+          }
+        }
+      );
     });
     });
 
 
     // copy checklists
     // copy checklists

+ 1 - 2
server/migrations.js

@@ -1061,6 +1061,7 @@ Migrations.add('change-attachment-library', () => {
     let opts = {
     let opts = {
     	fileName: file.name(),
     	fileName: file.name(),
 			type: file.type(),
 			type: file.type(),
+      size: file.size(),
       fileId: file._id,
       fileId: file._id,
 			meta: {
 			meta: {
 				userId: file.userId,
 				userId: file.userId,
@@ -1077,8 +1078,6 @@ Migrations.add('change-attachment-library', () => {
 		Attachments.addFile(path, opts, (err, fileRef) => {
 		Attachments.addFile(path, opts, (err, fileRef) => {
       if (err) {
       if (err) {
         console.log('error when migrating ', fileName, err);
         console.log('error when migrating ', fileName, err);
-      } else {
-        file.remove();
       }
       }
     });
     });
 	});
 	});