+ const [ totalBytes, totalBytesDaily ] = await Promise.all([
+ UserModel.getOriginalVideoFileTotalFromUser(this),
+ UserModel.getOriginalVideoFileTotalDailyFromUser(this)
+ ])
+
+ const uploadedTotal = videoFile.size + totalBytes
+ const uploadedDaily = videoFile.size + totalBytesDaily
+ if (this.videoQuotaDaily === -1) {
+ return uploadedTotal < this.videoQuota
+ }
+ if (this.videoQuota === -1) {
+ return uploadedDaily < this.videoQuotaDaily
+ }
+
+ return (uploadedTotal < this.videoQuota) &&
+ (uploadedDaily < this.videoQuotaDaily)
+ }
+
+ private static generateUserQuotaBaseSQL (where?: string) {
+ const andWhere = where ? 'AND ' + where : ''
+
+ return 'SELECT SUM("size") AS "total" ' +
+ 'FROM (' +
+ 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' +
+ 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' +
+ 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
+ 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
+ 'WHERE "account"."userId" = $userId ' + andWhere +
+ 'GROUP BY "video"."id"' +
+ ') t'
+ }
+
+ private static getTotalRawQuery (query: string, userId: number) {
+ const options = {
+ bind: { userId },
+ type: Sequelize.QueryTypes.SELECT
+ }
+
+ return UserModel.sequelize.query(query, options)
+ .then(([ { total } ]) => {
+ if (total === null) return 0
+
+ return parseInt(total, 10)
+ })