watch.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. var async = require('async');
  2. var Watch = require('../models/Watch');
  3. var Category = require('../models/Category');
  4. var Reference = require('../models/Reference');
  5. var mongoose = require('mongoose');
  6. var nodemailer = require('nodemailer');
  7. const bodyParser = require('body-parser');
  8. // Get Movie or Series
  9. exports.watchGet = function(req, res){
  10. Watch.findOne({ 'permalink': req.params.permalink }, function(err, w){
  11. if( req.params.ep_number ){
  12. ep_number = req.params.ep_number - 1;
  13. }
  14. else {
  15. ep_number = 0;
  16. }
  17. var isMobile = false
  18. var isDesktop = false
  19. // to test if desktop
  20. if (req.device.type == 'phone') {
  21. isMobile = true
  22. }
  23. else if (req.device.type == 'desktop' && w.layout != "serie") {
  24. // var isDesktop = true
  25. isMobile = true
  26. }
  27. else{
  28. isMobile = true
  29. }
  30. if(typeof req.headers.referer !== 'undefined'){
  31. if( (req.headers.referer.match(/^https?:\/\/([^\/]+\.)?libreflix\.org(\/|$)/i)) ||
  32. (req.headers.referer.match(/^https?:\/\/([^\/]+\.)?localhost:3998(\/|$)/i)) )
  33. {
  34. } else {
  35. return res.redirect('/i/'+ w.permalink);
  36. }
  37. } else {
  38. return res.redirect('/i/'+ w.permalink);
  39. }
  40. if (w.useWatchV2) {
  41. res.render('watchv2', {
  42. title: w.title,
  43. isMobile: isMobile,
  44. isDesktop: isDesktop,
  45. w_eps: w.eps[ep_number],
  46. next_episode: ep_number + 2,
  47. w: w
  48. })
  49. }
  50. else{
  51. res.render('watch', {
  52. w: w,
  53. title: w.title,
  54. layout: w.layout,
  55. subtitle: w.subtitle,
  56. sinopse: w.sinopse,
  57. year: w.year,
  58. imgbg: w.imgbg,
  59. video: w.video,
  60. thumb480: w.thumb480,
  61. thumb130: w.thumb130,
  62. runtime: w.runtime,
  63. eps: w.eps
  64. })
  65. }
  66. });
  67. };
  68. // GET New Production
  69. exports.newWatchGet = function(req, res) {
  70. if (!req.user) {
  71. return res.redirect('/login');
  72. }
  73. res.render('novo', {
  74. title: 'Criar Nova Produção'
  75. });
  76. };
  77. // POST New Production
  78. exports.newWatchPost = function(req, res, next) {
  79. var body = req.body;
  80. // para retornar depois do erro
  81. var form = {
  82. };
  83. var errors = req.validationErrors();
  84. if (errors) {
  85. req.flash('error', errors);
  86. return res.render('novo', {form: form});
  87. }
  88. Watch.findOne({ permalink: req.body.permalink }, function(err, watch) {
  89. if (watch) {
  90. req.flash('error', { msg: 'O permalink inserido já existe. Tente outro.' });
  91. return res.redirect('/novo');
  92. }
  93. // Para salvar no BD
  94. watch = new Watch({
  95. criador: req.user.id,
  96. permalink: req.body.permalink,
  97. layout: 'filme',
  98. featured : false,
  99. title: req.body.title,
  100. subtitle: req.body.subtitle,
  101. sinopse: req.body.sinopse,
  102. year: req.body.year,
  103. classind: req.body.classind,
  104. duration: req.body.duration,
  105. video: req.body.video,
  106. thumb480: req.body.thumb480,
  107. imgbg: req.body.imgbg,
  108. tags: req.body.tags,
  109. status: "pending"
  110. });
  111. watch.save(function(err) {
  112. //req.logIn(campanha, function(err) {
  113. req.flash('success', { msg: 'Muito obrigado por sua colaboração. Em breve a produção estará no ar. <3' });
  114. res.redirect('/');
  115. //});
  116. });
  117. });
  118. };
  119. // GET Tags
  120. exports.tagsGet = function(req, res){
  121. Watch.find({ 'tags': req.params.tags }, null, {sort: '-year'}, function(err, w){
  122. if (!w) {
  123. return res.redirect('/404');
  124. }
  125. else{
  126. res.render('home', {
  127. title: 'Tag',
  128. tag: req.params.tags,
  129. watch: w
  130. });
  131. }
  132. });
  133. };
  134. //Get Edit Watch
  135. exports.watchEdit = function(req, res){
  136. Category.find({}, null, {sort: 'title'},function(err, categories){
  137. Watch.findOne({ '_id': req.params._id }, function(err, w){
  138. Reference.find({ 'attachedToWatch': req.params._id }, function(err, reference) {
  139. if (!w) {
  140. return res.redirect('/404');
  141. }
  142. else{
  143. res.render('edit', {
  144. categories: categories,
  145. reference: reference,
  146. w: w
  147. });
  148. }
  149. });
  150. }).populate('modComments.moderator').populate('criador');
  151. });
  152. };
  153. // PUT or Update a Production
  154. exports.watchPut = function(req, res, next) {
  155. var body = req.body;
  156. Watch.findById(req.params._id, function(err, watch) {
  157. /* Para salvar no BD */
  158. /* Internal */
  159. watch.permalink = req.body.permalink;
  160. watch.layout = req.body.layout;
  161. watch.featured = req.body.featured;
  162. if (req.user.adm) {
  163. watch.criador = req.body.criador;
  164. watch.top = req.body.top;
  165. }
  166. watch.mod_message = req.body.mod_message;
  167. watch.downloadable = req.body.downloadable;
  168. watch.canwecopy = req.body.canwecopy;
  169. /* Basic */
  170. watch.title = req.body.title;
  171. watch.subtitle = req.body.subtitle;
  172. watch.original_title = req.body.original_title;
  173. watch.year = req.body.year;
  174. watch.duration = req.body.duration;
  175. watch.classind = req.body.classind;
  176. watch.sinopse = req.body.sinopse;
  177. /* More Info */
  178. watch.description = req.body.description;
  179. watch.license = req.body.license;
  180. watch.location.country.code = req.body.location_country.code;
  181. watch.location.country.code = req.body.location_country.split("|")[0];
  182. watch.location.country.name = req.body.location_country.split("|")[1];
  183. watch.location.state = req.body.location_state;
  184. watch.location.city = req.body.location_city;
  185. // watch.location.lat = req.body.location.lat;
  186. // watch.location.lon = req.body.location.lon;
  187. watch.crew.director = req.body.crew_director;
  188. watch.crew.screenplay = req.body.crew_screenplay;
  189. watch.crew.producer = req.body.crew_producer;
  190. watch.crew.cast = req.body.crew_cast;
  191. watch.crew.editor = req.body.crew_editor;
  192. watch.crew.other = req.body.crew_other;
  193. /* Images */
  194. watch.imgbg = req.body.imgbg;
  195. watch.thumb480 = req.body.thumb480;
  196. watch.thumb130 = req.body.thumb130;
  197. /* Video */
  198. watch.video = req.body.video;
  199. watch.trailer = req.body.trailer;
  200. watch.quality = req.body.quality;
  201. watch.audio_language = req.body.audio_language;
  202. watch.srt_language = req.body.srt_language;
  203. /* Files and Download*/
  204. watch.file.film = req.body.file_film;
  205. watch.file.trailer = req.body.file_trailer;
  206. watch.file.srt = req.body.file_srt;
  207. watch.subs.pt_br = req.body.subs_pt;
  208. watch.subs.es = req.body.subs_es;
  209. watch.subs.en = req.body.subs_en;
  210. /* Categories */
  211. watch.tags = req.body.tags;
  212. watch.format = req.body.format;
  213. watch.categories = req.body['categories[]'];
  214. /* External Links */
  215. watch.links.website = req.body.website;
  216. console.log(req.body.website);
  217. console.log(watch.links.website);
  218. watch.links.wikipedia = req.body.wikipedia;
  219. watch.links.twitter = req.body.twitter;
  220. watch.links.imdb = req.body.imdb;
  221. watch.links.filmow = req.body.filmow;
  222. watch.links.facebook = req.body.facebook;
  223. watch.links.instagram = req.body.instagram;
  224. /* ModComments */
  225. watch.modComments.moderator = req.user.id;
  226. if (req.user.mod == true || req.user.adm == true) {
  227. watch.modComments.status = req.body.modComments_status;
  228. watch.status = req.body.modComments_status;
  229. }
  230. watch.modComments.comment = req.body.modComments_comment;
  231. if (req.body.modComments_status_old != req.body.modComments_status) {
  232. async.waterfall([
  233. function() {
  234. var transporter = nodemailer.createTransport({
  235. service: 'Mailgun',
  236. auth: {
  237. user: process.env.MAILGUN_USERNAME,
  238. pass: process.env.MAILGUN_PASSWORD
  239. }
  240. });
  241. var mailOptions = {
  242. to: req.body.criador_email,
  243. from: 'libreflix@protonmail.com',
  244. subject: 'Alteração de status de obra no Libreflix',
  245. html: 'Olá, amigx criador!' +
  246. '<br>O status da sua obra <b>' + req.body.title + '</b> foi alterado no Libreflix.' +
  247. '<br><br>Status anterior: ' + req.body.modComments_status_old +
  248. '<br>Status atual: ' + req.body.modComments_status +
  249. '<br><br>Comentário de um Librerian:<br> <pre>' + req.body.modComments_comment + '</pre>' +
  250. '<br><br>Muito obrigado por criar o Libreflix junto com a gente.' +
  251. '<br><br>Abraços Libres! <3<br>Time Libreflix<br><a href="https://libreflix.org"><img src="https://libreflix.org/libreflix.png" width="100"></a>'
  252. };
  253. transporter.sendMail(mailOptions, function(err) {});
  254. }
  255. ]);
  256. }
  257. watch.save(function(err) {
  258. req.flash('success', { msg: 'Alterações feitas com sucesso.' });
  259. res.redirect('/edit/' + req.params._id);
  260. });
  261. });
  262. };
  263. exports.newReference = function(req, res, next) {
  264. if (req.xhr || req.accepts('json,html') === 'json') {
  265. console.log('OI');
  266. console.log(req.body.u);
  267. console.log(req.body.ref_url);
  268. console.log(req.body.ref_title);
  269. if (req.body.ref_url && req.body.ref_title) {
  270. Watch.findOne({ 'permalink': req.params.permalink }, function(err, watch){
  271. reference = new Reference({
  272. attachedToWatch: watch.id,
  273. creator: req.body.u,
  274. url: req.body.ref_url,
  275. title: req.body.ref_title
  276. });
  277. reference.save(function(err) {
  278. res.send({success: true})
  279. });
  280. })
  281. } else {
  282. res.send({success: false})
  283. }
  284. }
  285. }