Ref: original & and use fileObj.meta

fileObj.meta is part of the ostrio:files API and be passed to the
constructor. This is less hacky than trying tu update a persistet object
after the fact.
This commit is contained in:
David Arnold 2020-09-14 01:07:17 -05:00 committed by Denis Perov
parent 16506e7a6a
commit e702f17c7b
10 changed files with 124 additions and 159 deletions

View file

@ -242,7 +242,7 @@ if (Meteor.isServer) {
}
if (activity.attachmentId) {
const attachment = activity.attachment();
params.attachment = attachment.original.name;
params.attachment = attachment.name;
params.attachmentId = attachment._id;
}
if (activity.checklistId) {

View file

@ -7,6 +7,21 @@ import { createOnAfterRemove } from './lib/fsHooks/createOnAfterRemove';
const attachmentBucket = createBucket('attachments');
const insertActivity = (fileObj, activityType) =>
Activities.insert({
userId: fileObj.userId,
type: 'card',
activityType,
attachmentId: fileObj._id,
// this preserves the name so that notifications can be meaningful after
// this file is removed
attachmentName: fileObj.name,
boardId: fileObj.meta.boardId,
cardId: fileObj.meta.cardId,
listId: fileObj.meta.listId,
swimlaneId: fileObj.meta.swimlaneId,
});
// XXX Enforce a schema for the Attachments FilesCollection
// see: https://github.com/VeliovGroup/Meteor-Files/wiki/Schema
@ -14,84 +29,46 @@ export const Attachments = new FilesCollection({
debug: false, // Change to `true` for debugging
collectionName: 'attachments',
allowClientCode: false,
onAfterUpload(doc) {
onAfterUpload(fileRef) {
createOnAfterUpload(attachmentBucket)(fileRef);
// If the attachment doesn't have a source field
// or its source is different than import
if (!doc.source || doc.source !== 'import') {
if (!fileRef.meta.source || fileRef.meta.source !== 'import') {
// Add activity about adding the attachment
Activities.insert({
userId,
type: 'card',
activityType: 'addAttachment',
attachmentId: doc._id,
// this preserves the name so that notifications can be meaningful after
// this file is removed
attachmentName: doc.original.name,
boardId: doc.boardId,
cardId: doc.cardId,
listId: doc.listId,
swimlaneId: doc.swimlaneId,
});
} else {
// Don't add activity about adding the attachment as the activity
// be imported and delete source field
Attachments.update(
{
_id: doc._id,
},
{
$unset: {
source: '',
},
},
);
insertActivity(fileRef, 'addAttachment');
}
createOnAfterUpload(attachmentBucket)(doc);
},
interceptDownload: createInterceptDownload(attachmentBucket),
onAfterRemove(docs) {
docs.forEach(function(doc) {
Activities.insert({
userId: doc.userId,
type: 'card',
activityType: 'deleteAttachment',
attachmentId: doc._id,
// this preserves the name so that notifications can be meaningful after
// this file is removed
attachmentName: doc.original.name,
boardId: doc.boardId,
cardId: doc.cardId,
listId: doc.listId,
swimlaneId: doc.swimlaneId,
});
onAfterRemove(files) {
createOnAfterRemove(attachmentBucket)(files);
files.forEach(fileObj => {
insertActivity(fileObj, 'deleteAttachment');
});
createOnAfterRemove(attachmentBucket)(docs);
},
// We authorize the attachment download either:
// - if the board is public, everyone (even unconnected) can download it
// - if the board is private, only board members can download it
downloadCallback(doc) {
const board = Boards.findOne(doc.boardId);
protected(fileObj) {
const board = Boards.findOne(fileObj.meta.boardId);
if (board.isPublic()) {
return true;
} else {
return board.hasMember(this.userId);
}
return board.hasMember(this.userId);
},
});
if (Meteor.isServer) {
Attachments.allow({
insert(userId, doc) {
return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
insert(userId, fileObj) {
return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
},
update(userId, doc) {
return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
update(userId, fileObj) {
return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
},
remove(userId, doc) {
return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
remove(userId, fileObj) {
return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
},
fetch: ['boardId'],
fetch: ['meta'],
});
Meteor.startup(() => {

View file

@ -78,11 +78,11 @@ export class Exporter {
return {
_id: attachment._id,
cardId: attachment.cardId,
cardId: attachment.meta.cardId,
//url: FlowRouter.url(attachment.url()),
file: filebase64,
name: attachment.original.name,
type: attachment.original.type,
name: attachment.name,
type: attachment.type,
};
});
//When has a especific valid attachment return the single element
@ -209,7 +209,7 @@ export class Exporter {
delimiter: userDelimiter,
header: true,
newline: "\r\n",
skipEmptyLines: false,
skipEmptyLines: false,
escapeFormulae: true,
};

View file

@ -422,35 +422,33 @@ export class TrelloCreator {
}
const attachments = this.attachments[card.id];
const trelloCoverId = card.idAttachmentCover;
// Simulating file.attachData on the client generates multiple errors
// - HEAD returns null, which causes exception down the line
// - the template then tries to display the url to the attachment which causes other errors
// so we make it server only, and let UI catch up once it is done, forget about latency comp.
if (attachments && Meteor.isServer) {
attachments.forEach(att => {
// Simulating file.attachData on the client generates multiple errors
// - HEAD returns null, which causes exception down the line
// - the template then tries to display the url to the attachment which causes other errors
// so we make it server only, and let UI catch up once it is done, forget about latency comp.
const self = this;
const opts = {
type: att.type ? att.type : undefined,
userId: self._user(att.userId),
meta: {
boardId,
cardId,
source: 'import',
},
};
const cb = (error, fileObj) => {
if (error) {
throw error;
}
self.attachmentIds[att._id] = fileObj._id;
if (trelloCoverId === att._id) {
Cards.direct.update(cardId, {
$set: { coverId: fileObj._id },
});
}
};
if (att.url) {
Attachment.load(att.url, (error, fileObj) => {
if (error) {
throw error;
}
fileObj.boardId = boardId;
fileObj.cardId = cardId;
fileObj.userId = self._user(att.userId);
// The field source will only be used to prevent adding
// attachments' related activities automatically
fileObj.source = 'import';
self.attachmentIds[att._id] = fileObj._id;
if (trelloCoverId === att.id) {
Cards.direct.update(cardId, {
$set: { coverId: fileObj._id },
});
}
});
Attachment.load(att.url, opts, cb, true);
} else if (att.file) {
Attachment.write(att.file, opts, cb, true);
}
});

View file

@ -446,33 +446,31 @@ export class WekanCreator {
const wekanCoverId = card.coverId;
if (attachments && Meteor.isServer) {
attachments.forEach(att => {
// Simulating file.attachData on the client generates multiple errors
// - HEAD returns null, which causes exception down the line
// - the template then tries to display the url to the attachment which causes other errors
// so we make it server only, and let UI catch up once it is done, forget about latency comp.
const self = this;
if (att.url || att.file) {
Attachment.load(
att.url ? att.url : Buffer.from(att.file, 'base64'),
{ type: att.type ? att.ype : undefined },
(error, fileObj) => {
if (error) {
throw error;
}
fileObj.boardId = boardId;
fileObj.cardId = cardId;
fileObj.userId = self._user(att.userId);
// The field source will only be used to prevent adding
// attachments' related activities automatically
fileObj.source = 'import';
self.attachmentIds[att._id] = fileObj._id;
if (wekanCoverId === att._id) {
Cards.direct.update(cardId, {
$set: { coverId: fileObj._id },
});
}
},
);
const opts = {
type: att.type ? att.type : undefined,
userId: self._user(att.userId),
meta: {
boardId,
cardId,
source: 'import',
},
};
const cb = (error, fileObj) => {
if (error) {
throw error;
}
self.attachmentIds[att._id] = fileObj._id;
if (wekanCoverId === att._id) {
Cards.direct.update(cardId, {
$set: { coverId: fileObj._id },
});
}
};
if (att.url) {
Attachment.load(att.url, opts, cb, true);
} else if (att.file) {
Attachment.write(att.file, opts, cb, true);
}
});
}