|
| 1 | +// Import meteor packages |
| 2 | +import { Meteor } from 'meteor/meteor'; |
| 3 | +import { sAlert } from 'meteor/juliancwirko:s-alert'; |
| 4 | +import { TAPi18n } from 'meteor/tap:i18n'; |
| 5 | + |
| 6 | +// Import apinf collections |
| 7 | +import { Branding } from '/branding/collection'; |
| 8 | +import CoverPhoto from '/branding/cover_photo/collection'; |
| 9 | +import { fileNameEndsWith } from '/core/helper_functions/file_name_ends_with'; |
| 10 | + |
| 11 | +Meteor.startup(function () { |
| 12 | + // Set cover photo id to branding collection on Success |
| 13 | + CoverPhoto.resumable.on('fileSuccess', function (file) { |
| 14 | + |
| 15 | + // Get the id from project logo file object |
| 16 | + const coverPhotoFileId = file.uniqueIdentifier; |
| 17 | + |
| 18 | + // Get branding |
| 19 | + const branding = Branding.findOne(); |
| 20 | + |
| 21 | + // Update logo id field |
| 22 | + Branding.update(branding._id, { $set: { coverPhotoFileId } }); |
| 23 | + |
| 24 | + // Get upload success message translation |
| 25 | + const message = TAPi18n.__('uploadCoverPhoto_successfully_uploaded'); |
| 26 | + |
| 27 | + // Alert user of successful upload |
| 28 | + sAlert.success(message); |
| 29 | + }); |
| 30 | + |
| 31 | + CoverPhoto.resumable.on('fileAdded', function (file) { |
| 32 | + return CoverPhoto.insert({ |
| 33 | + _id: file.uniqueIdentifier, |
| 34 | + filename: file.fileName, |
| 35 | + contentType: file.file.type, |
| 36 | + }, function (err) { |
| 37 | + if (err) { |
| 38 | + // Create & show a message about failed |
| 39 | + const message = `${TAPi18n.__('uploadCoverPhoto_acceptedExtensions_errorText')} ${err}`; |
| 40 | + sAlert.warning(message) |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + // Available extensions for pictures |
| 45 | + const acceptedExtensions = ['jpg', 'jpeg', 'png', 'gif']; |
| 46 | + |
| 47 | + // Check extensions for uploading file: is it a picture or not? |
| 48 | + if (fileNameEndsWith(file.file.name, acceptedExtensions)) { |
| 49 | + // Upload the cover photo |
| 50 | + return CoverPhoto.resumable.upload(); |
| 51 | + } |
| 52 | + |
| 53 | + // Get extension error message |
| 54 | + const message = TAPi18n.__('uploadCoverPhoto_acceptedExtensions'); |
| 55 | + |
| 56 | + // Alert user of extension error |
| 57 | + sAlert.error(message); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
0 commit comments