Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 4 additions & 85 deletions controllers/frontend/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const { saveAndServeFilesDirectory } = require('../../lib/helpers/settings');

const { userCanUploadContentOfThisRating } = require('../../lib/uploading/helpers');

const {getEnglishStringOrder, sortUploads} = require('../../lib/account/order');

const validator = require('email-validator');

const { getUploadDuration } = require('../../lib/mediaBrowsing/helpers');
Expand All @@ -51,37 +53,6 @@ const timeAgoEnglish = new javascriptTimeAgo('en-US');

const secondsToFormattedTime = timeHelper.secondsToFormattedTime;

// TODO: pull this function out
async function addValuesIfNecessary(upload, channelUrl){
if(upload.fileType == 'video' || upload.fileType == 'audio'){
if(!upload.durationInSeconds || !upload.formattedDuration){

var server = uploadServer;
if(server.charAt(0) == '/') // the slash confuses the file reading, because host root directory is not the same as machine root directory
server = server.substr(1);

const uploadLocation = `${server}/${channelUrl}/${upload.uniqueTag + upload.fileExtension}`;

try {
const duration = await getUploadDuration(uploadLocation, upload.fileType);
console.log(duration);

let uploadDocument = await Upload.findOne({uniqueTag: upload.uniqueTag});

uploadDocument.durationInSeconds = duration.seconds;
uploadDocument.formattedDuration = duration.formattedTime;

await uploadDocument.save();

} catch(err){
/** if the file has been deleted then it won't blow up **/
// console.log(err);
}
// console.log('have to add');
}
}
}

/**
* GET /upload
* Page to facilitate user uploads
Expand Down Expand Up @@ -389,30 +360,7 @@ exports.getChannel = async(req, res) => {
orderBy = req.query.orderBy;
}

// console.log(`orderBy : ${orderBy}`)

if(orderBy !== 'popular' && orderBy !== 'newToOld' && orderBy !== 'oldToNew' && orderBy !== 'alphabetical'){
console.log('doesnt connect');
orderBy = 'newToOld';
}

let orderByEnglishString;

if(orderBy == 'alphabetical'){
orderByEnglishString = 'Alphabetical';
}

if(orderBy == 'oldToNew'){
orderByEnglishString = 'Old To New';
}

if(orderBy == 'newToOld'){
orderByEnglishString = 'New To Old';
}

if(orderBy == 'popular'){
orderByEnglishString = 'Popular';
}
let orderByEnglishString = getEnglishStringOrder(orderBy);

let alreadySubbed = false;

Expand Down Expand Up @@ -445,30 +393,7 @@ exports.getChannel = async(req, res) => {

const userUploadAmount = uploads.length;

if(orderBy == 'newToOld'){

// console.log('new to old');
uploads = uploads.sort(function(a, b){
return b.createdAt - a.createdAt;
});
}

if(orderBy == 'oldToNew'){

// console.log('old to new');
uploads = uploads.sort(function(a, b){
return a.createdAt - b.createdAt;
});
}

if(orderBy == 'alphabetical'){

// console.log('alphabetical');

uploads = uploads.sort(function(a, b){
return a.title.localeCompare(b.title);
});
}
uploads = sortUploads(orderBy, uploads);

let filter = uploadFilters.getSensitivityFilter(req.user, req.siteVisitor);

Expand All @@ -488,12 +413,6 @@ exports.getChannel = async(req, res) => {
})
);

if(orderBy == 'popular'){
uploads = uploads.sort(function(a, b){
return b.legitViewAmount - a.legitViewAmount;
});
}

let totalViews = 0;
for(upload of uploads){
totalViews = totalViews + upload.legitViewAmount;
Expand Down
32 changes: 32 additions & 0 deletions lib/account/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
async function addValuesIfNecessary(upload, channelUrl){
if(upload.fileType == 'video' || upload.fileType == 'audio'){
if(!upload.durationInSeconds || !upload.formattedDuration){

var server = uploadServer;
if(server.charAt(0) == '/') // the slash confuses the file reading, because host root directory is not the same as machine root directory
server = server.substr(1);

const uploadLocation = `${server}/${channelUrl}/${upload.uniqueTag + upload.fileExtension}`;

try {
const duration = await getUploadDuration(uploadLocation, upload.fileType);
console.log(duration);

let uploadDocument = await Upload.findOne({uniqueTag: upload.uniqueTag});

uploadDocument.durationInSeconds = duration.seconds;
uploadDocument.formattedDuration = duration.formattedTime;

await uploadDocument.save();

} catch(err){
/** if the file has been deleted then it won't blow up **/
// console.log(err);
}
// console.log('have to add');
}
}
}


module.exports = addValuesIfNecessary;
67 changes: 67 additions & 0 deletions lib/account/order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
function getEnglishStringOrder(orderBy){

let orderByEnglishString;

if(orderBy == 'alphabetical'){
orderByEnglishString = 'Alphabetical';
}

if(orderBy == 'oldToNew'){
orderByEnglishString = 'Old To New';
}

if(orderBy == 'newToOld'){
orderByEnglishString = 'New To Old';
}

if(orderBy == 'popular'){
orderByEnglishString = 'Popular';
}

return orderByEnglishString;
}

function sortUploads(orderBy, unsortedUploads) {

let uploads;

if(orderBy == 'newToOld'){

// console.log('new to old');
uploads = unsortedUploads.sort(function(a, b){
return b.createdAt - a.createdAt;
});
}

if(orderBy == 'oldToNew'){

// console.log('old to new');
uploads = unsortedUploads.sort(function(a, b){
return a.createdAt - b.createdAt;
});
}

if(orderBy == 'alphabetical'){

// console.log('alphabetical');

uploads = unsortedUploads.sort(function(a, b){
return a.title.localeCompare(b.title);
});
}

if(orderBy == 'popular'){
uploads = unsortedUploads.sort(function(a, b){
return b.legitViewAmount - a.legitViewAmount;
});
}

return uploads;

}


module.exports = {
getEnglishStringOrder: getEnglishStringOrder,
sortUploads : sortUploads
}