@@ -10,6 +10,8 @@ const path = require('path');
1010const mkdirp = Promise . promisifyAll ( require ( 'mkdirp' ) ) ;
1111var randomstring = require ( 'randomstring' ) ;
1212var CombinedStream = require ( 'combined-stream' ) ;
13+ const FileType = require ( 'file-type' ) ;
14+ const readChunk = require ( 'read-chunk' ) ;
1315
1416const redisClient = require ( '../../config/redis' ) ;
1517
@@ -165,7 +167,8 @@ function testIfUserRestricted(user, logObject, res){
165167function aboutToProcess ( res , channelUrl , uniqueTag ) {
166168 res . send ( {
167169 message : 'ABOUT TO PROCESS' ,
168- url : `/user/${ channelUrl } /${ uniqueTag } ?u=t`
170+ url : `/user/${ channelUrl } /${ uniqueTag } ?u=t` ,
171+ uniqueTag
169172 } ) ;
170173}
171174
@@ -417,6 +420,8 @@ exports.postFileUpload = async(req, res) => {
417420
418421 uploadLogger . info ( 'Concat done' , logObject ) ;
419422
423+ // TODO: pull this out into its own function
424+
420425 let bitrate , codecName , codecProfile ;
421426
422427 if ( upload . fileType !== 'image' ) {
@@ -587,25 +592,32 @@ exports.postFileUpload = async(req, res) => {
587592
588593 uploadLogger . info ( 'Upload marked as complete' , logObject ) ;
589594
590- updateUsersUnreadSubscriptions ( user ) ;
591-
592- uploadLogger . info ( 'Updated subscribed users subscriptions' , logObject ) ;
593-
594595 // this is admin upload for all
595596 alertAdminOfNewUpload ( user , upload ) ;
596597
597598 uploadLogger . info ( 'Alert admins of a new upload' , logObject ) ;
598599
599- if ( upload . visibility == 'public' ) {
600- // update user push notifications
601- updateUsersPushNotifications ( user , upload ) ;
600+ // if visibility is public, send push and email notifications
601+ if ( upload . visibility === 'public' ) {
602+
603+ // update the subscription amount of subscribing users
604+ updateUsersUnreadSubscriptions ( user ) ;
605+
606+ uploadLogger . info ( 'Updated subscribed users subscriptions' , logObject ) ;
607+
608+ // send push and email notifs if production
609+ if ( process . env . NODE_ENV === 'production' ) {
610+ // update user push notifications
611+ updateUsersPushNotifications ( user , upload ) ;
602612
603- uploadLogger . info ( 'Update users push notifications' , logObject ) ;
613+ uploadLogger . info ( 'Update users push notifications' , logObject ) ;
604614
605- // update user email notifications
606- updateUsersEmailNotifications ( user , upload , req . host ) ;
615+ // update user email notifications
616+ updateUsersEmailNotifications ( user , upload , req . host ) ;
617+
618+ uploadLogger . info ( 'Update users email notifications' , logObject ) ;
619+ }
607620
608- uploadLogger . info ( 'Update users email notifications' , logObject ) ;
609621 }
610622
611623 // upload is complete, send it off to user (aboutToProcess is a misnomer here)
@@ -732,3 +744,92 @@ exports.adminUpload = async(req, res) => {
732744 // });
733745
734746} ;
747+
748+ /**
749+ * POST /api/uploadFileThumbnail
750+ * Upload file thumbnail
751+ */
752+ exports . postThumbnailUpload = async ( req , res ) => {
753+ console . log ( 'files' ) ;
754+ console . log ( req . files ) ;
755+
756+ console . log ( 'body' ) ;
757+ console . log ( req . body ) ;
758+
759+ const uniqueTag = req . body . uploadUniqueTag ;
760+
761+ // check if there's a thumbnail
762+ let thumbnailFile ;
763+
764+ // if req there are files and
765+ if ( req . files && req . files . thumbnailFile ) {
766+ thumbnailFile = req . files . thumbnailFile ;
767+ } else {
768+ res . status ( 500 ) ;
769+ return res . send ( 'no thumbnail file' ) ;
770+ }
771+
772+ const filename = thumbnailFile . originalFilename ;
773+ // size in bytes
774+ const fileSize = thumbnailFile . size ;
775+
776+ // 5 MB
777+ if ( fileSize > 5242880 ) {
778+ res . status ( 500 ) ;
779+ return res . send ( 'file too large' ) ;
780+ }
781+
782+ const filePath = thumbnailFile . path ;
783+
784+ const buffer = readChunk . sync ( filePath , 0 , 4100 ) ;
785+
786+ const bufferFileType = await FileType . fromBuffer ( buffer ) ;
787+
788+ console . log ( 'Buffer file type ' + bufferFileType ) ;
789+
790+ // comes back at 'mp4' to prepend . to make .mp4
791+ const extension = '.' + String ( bufferFileType . ext ) ;
792+
793+ console . log ( 'extension' ) ;
794+ console . log ( extension ) ;
795+
796+ const fileType = getMediaType ( 'hackforsetup' + extension ) ;
797+
798+ if ( fileType !== 'image' ) {
799+ res . status ( 500 ) ;
800+ return res . send ( 'not an image' ) ;
801+ }
802+
803+ console . log ( 'File type' ) ;
804+ console . log ( fileType ) ;
805+
806+ console . log ( bufferFileType ) ;
807+
808+ const channelUrl = req . user . channelUrl ;
809+
810+ const saveFileDirectory = `${ saveAndServeFilesDirectory } /${ channelUrl } /${ uniqueTag } -custom${ extension } ` ;
811+
812+ await fs . move ( filePath , saveFileDirectory ) ;
813+
814+ const upload = await Upload . findOne ( { uniqueTag } ) ;
815+
816+ if ( ! upload . thumbnails ) {
817+ upload . thumbnails = { } ;
818+ }
819+
820+ upload . thumbnails . custom = `${ uniqueTag } -custom${ extension } ` ;
821+
822+ if ( process . env . UPLOAD_TO_B2 === 'true' ) {
823+ // TODO: check this
824+ // await backblaze.editploadThumbnailToB2(req.user.channelUrl, upload.uniqueTag, extension, saveFileDirectory);
825+ }
826+
827+ // sendUploadThumbnailToB2(args)
828+
829+ await upload . save ( ) ;
830+
831+ res . status ( 200 ) ;
832+
833+ return res . send ( 'success' ) ;
834+
835+ } ;
0 commit comments