Skip to content

Commit 4211029

Browse files
authored
Merge pull request #413 from mayeaux/add-thumbnail-upload
Add thumbnail upload
2 parents 5ce51ef + aec77c7 commit 4211029

File tree

11 files changed

+705
-406
lines changed

11 files changed

+705
-406
lines changed

app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ if(cluster.isMaster){
227227
requestPath === '/upload' ||
228228
requestPath === '/account/profile' ||
229229
requestPath === '/api/channel/thumbnail/delete' ||
230+
requestPath === '/api/uploadFileThumbnail' ||
230231
requestPath.match(editUploadRegexp) ||
231232
requestPath.match(deleteUploadThumbnailRegexp) ||
232233
requestPath === '/livestream/on-live-auth' ||

controllers/backend/uploading.js

Lines changed: 113 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const path = require('path');
1010
const mkdirp = Promise.promisifyAll(require('mkdirp'));
1111
var randomstring = require('randomstring');
1212
var CombinedStream = require('combined-stream');
13+
const FileType = require('file-type');
14+
const readChunk = require('read-chunk');
1315

1416
const redisClient = require('../../config/redis');
1517

@@ -165,7 +167,8 @@ function testIfUserRestricted(user, logObject, res){
165167
function 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+
};

controllers/frontend/account.js

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ const forgotEmailFunctionalityOn = process.env.FORGOT_PASSWORD_EMAIL_FUNCTIONALI
5959

6060
const { attachDataToUploadsAsUploads } = require('../../lib/helpers/addFieldsToUploads');
6161

62+
const stripeToken = process.env.STRIPE_FRONTEND_TOKEN;
63+
64+
const plusEnabled = process.env.PLUS_ENABLED === 'true';
65+
66+
const verifyEmailFunctionalityOn = process.env.CONFIRM_EMAIL_FUNCTIONALITY_ON === 'true';
67+
6268
// TODO: pull this function out
6369
function removeTrailingSlash(requestPath){
6470
if(requestPath.charAt(requestPath.length - 1) == '/'){
@@ -68,37 +74,6 @@ function removeTrailingSlash(requestPath){
6874
return requestPath;
6975
}
7076

71-
// TODO: pull this function out
72-
async function addValuesIfNecessary(upload, channelUrl){
73-
if(upload.fileType == 'video' || upload.fileType == 'audio'){
74-
if(!upload.durationInSeconds || !upload.formattedDuration){
75-
76-
var server = uploadServer;
77-
if(server.charAt(0) == '/') // the slash confuses the file reading, because host root directory is not the same as machine root directory
78-
server = server.substr(1);
79-
80-
const uploadLocation = `${server}/${channelUrl}/${upload.uniqueTag + upload.fileExtension}`;
81-
82-
try {
83-
const duration = await getUploadDuration(uploadLocation, upload.fileType);
84-
console.log(duration);
85-
86-
let uploadDocument = await Upload.findOne({uniqueTag: upload.uniqueTag});
87-
88-
uploadDocument.durationInSeconds = duration.seconds;
89-
uploadDocument.formattedDuration = duration.formattedTime;
90-
91-
await uploadDocument.save();
92-
93-
} catch(err){
94-
/** if the file has been deleted then it won't blow up **/
95-
// console.log(err);
96-
}
97-
// console.log('have to add');
98-
}
99-
}
100-
}
101-
10277
/**
10378
* GET /upload
10479
* Page to facilitate user uploads
@@ -124,7 +99,8 @@ exports.getFileUpload = async(req, res) => {
12499
categories,
125100
maxRatingAllowed: process.env.MAX_RATING_ALLOWED,
126101
userCanUploadContentOfThisRating,
127-
secondsToFormattedTime
102+
secondsToFormattedTime,
103+
plusEnabled
128104
});
129105
};
130106

@@ -903,11 +879,6 @@ exports.getSignup = (req, res) => {
903879
* Account page.
904880
*/
905881
exports.getAccount = async(req, res) => {
906-
const stripeToken = process.env.STRIPE_FRONTEND_TOKEN;
907-
908-
const plusEnabled = process.env.PLUS_ENABLED == 'true';
909-
910-
const verifyEmailFunctionalityOn = process.env.CONFIRM_EMAIL_FUNCTIONALITY_ON == 'true';
911882

912883
// give user an upload token
913884
if(!req.user.uploadToken){

controllers/frontend/mediaPlayer.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@ function getFormattedFileSize(upload){
7474
*/
7575
exports.getMedia = async(req, res) => {
7676

77+
// TODO: pull out plus redirect thing
78+
7779
// get the amount of slashes, to determine if the user is allowed
7880
// to access the vanity url version of this url
7981
let requestPath = req.path;
8082

83+
var queryParams = req.url.split('?')[1];
84+
8185
if(requestPath.charAt(requestPath.length - 1) == '/'){
8286
requestPath = requestPath.substr(0, requestPath.length - 1);
8387
}
@@ -118,10 +122,17 @@ exports.getMedia = async(req, res) => {
118122
});
119123
}
120124

125+
// console.log(req.path);
126+
121127
// TODO: make sure to add query params here
122128
// if it's three but you're plus, then move to shortened url
123129
if(amountOfSlashes === 3 && user.plan == 'plus'){
124-
return res.redirect(`/${user.channelUrl}/${upload.uniqueTag}`);
130+
let redirectPath = `/${user.channelUrl}/${upload.uniqueTag}`;
131+
if(queryParams){
132+
redirectPath = redirectPath + '?' + queryParams;
133+
}
134+
135+
return res.redirect(redirectPath);
125136
}
126137

127138
// TODO: pull this thing out

package-lock.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
"paypal-rest-sdk": "^1.8.1",
109109
"pug": "^2.0.3",
110110
"randomstring": "^1.1.5",
111+
"read-chunk": "^3.2.0",
111112
"recaptcha2": "^1.3.3",
112113
"redis": "^2.8.0",
113114
"request": "^2.88.0",

routes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ function frontendRoutes(app){
259259
app.post('/api/upload/:uniqueTag/edit', passportConfig.isAuthenticated, internalApiController.editUpload);
260260
app.post('/api/upload/:uniqueTag/thumbnail/delete', passportConfig.isAuthenticated, internalApiController.deleteUploadThumbnail);
261261
app.post('/api/upload/:uniqueTag/captions/delete', passportConfig.isAuthenticated, internalApiController.deleteUploadCaption);
262+
app.post('/api/uploadFileThumbnail', passportConfig.isAuthenticated, uploadingController.postThumbnailUpload);
262263

263264
/** API ENDPOINTS **/
264265
app.post('/api/react/:upload/:user', passportConfig.isAuthenticated, internalApiController.react);

0 commit comments

Comments
 (0)