PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app.js

https://bitbucket.org/alex-di/printer-sprinter-server
JavaScript | 338 lines | 279 code | 55 blank | 4 comment | 21 complexity | fdbccb73aded470174147cfb1ff7200f MD5 | raw file
  1. // Generated by CoffeeScript 1.7.1
  2. /*
  3. Module dependencies.
  4. */
  5. (function() {
  6. var Image, Subscription, SubscriptionScheme, app, auth, config, express, fs, http, ig, io, media, mediaProcessingBlock, mongoose, path, routes, server, socketIO, socketIOstream;
  7. express = require("express");
  8. config = require("./config");
  9. routes = require("./routes");
  10. http = require("http");
  11. path = require("path");
  12. fs = require("fs");
  13. mongoose = require("mongoose");
  14. Image = require("./models/image.js");
  15. global.config = config;
  16. ig = require("instagram-node").instagram();
  17. media = require("./media").media;
  18. mediaProcessingBlock = false;
  19. SubscriptionScheme = new mongoose.Schema({
  20. tag: String,
  21. additional: String,
  22. sid: String
  23. });
  24. SubscriptionScheme.pre("save", function(next) {
  25. var counter, self;
  26. counter = 0;
  27. self = this;
  28. ig.add_tag_subscription(this.tag, config.instagram.listen, function(err, result, limit) {
  29. if (err) {
  30. console.log(err);
  31. ++counter;
  32. if (counter < 5) {
  33. err.retry();
  34. } else {
  35. err = new Error("IG lost connection");
  36. next(err);
  37. }
  38. return;
  39. }
  40. self.sid = result.id;
  41. next();
  42. });
  43. });
  44. SubscriptionScheme.pre("remove", function(next) {
  45. return ig.del_subscription({
  46. id: this.sid
  47. }, function() {
  48. return next();
  49. });
  50. });
  51. Subscription = mongoose.model("Subscription", SubscriptionScheme);
  52. global.Subscription = Subscription;
  53. global.mediaProccessingBlock = mediaProcessingBlock;
  54. ig.use(config.instagram);
  55. global.ig = ig;
  56. app = express();
  57. app.set("port", process.env.PORT || 4012);
  58. app.set("views", path.join(__dirname, "views"));
  59. app.set("view engine", "jade");
  60. app.use(express.favicon());
  61. app.use(express.logger("dev"));
  62. app.use(express.json());
  63. app.use(express.urlencoded());
  64. app.use(express.methodOverride());
  65. app.use(app.router);
  66. app.use(require("stylus").middleware(path.join(__dirname, "public")));
  67. app.use(express["static"](path.join(__dirname, "public")));
  68. mongoose.connect(config.db);
  69. if ("development" === app.get("env")) {
  70. app.use(express.errorHandler());
  71. }
  72. socketIO = require("socket.io");
  73. socketIOstream = require("socket.io-stream");
  74. global.io = socketIO;
  75. routes.add = require("./routes/add").add;
  76. routes.gallery = require("./routes/gallery");
  77. auth = express.basicAuth(function(user, pass) {
  78. return user === "admin" && pass === "admTBWA";
  79. });
  80. app.get("/", auth, function(req, res) {
  81. var tries;
  82. tries = 0;
  83. return ig.subscriptions(function(err, result, limit) {
  84. var item, tags;
  85. if (err) {
  86. console.log("Subscriptions read err:", err);
  87. ++tries;
  88. if (tries < 10) {
  89. err.retry();
  90. } else {
  91. res.render("index", {
  92. title: "Subscriptions",
  93. subscriptions: []
  94. });
  95. }
  96. } else {
  97. console.log("Result at 91", result);
  98. }
  99. if (result) {
  100. tags = (function() {
  101. var _i, _len, _results;
  102. _results = [];
  103. for (_i = 0, _len = result.length; _i < _len; _i++) {
  104. item = result[_i];
  105. _results.push(item.object_id);
  106. }
  107. return _results;
  108. })();
  109. Subscription.remove({
  110. "tag": {
  111. $nin: tags
  112. }
  113. }).exec(function(err) {
  114. if (err) {
  115. return console.log(err);
  116. }
  117. });
  118. return Subscription.find({}, function(err, subs) {
  119. return res.render("index", {
  120. title: "Subscriptions",
  121. subscriptions: subs
  122. });
  123. });
  124. }
  125. });
  126. });
  127. app.post("/listen", routes.add);
  128. app.get("/gallery", routes.gallery);
  129. app.get("/listen", function(req, res) {
  130. res.send(200, req.query["hub.challenge"]);
  131. });
  132. server = http.createServer(app).listen(app.get("port"), function() {
  133. console.log("Express server listening on port " + app.get("port"));
  134. });
  135. io = socketIO.listen(server);
  136. io.set("log level", 1);
  137. global.io = io;
  138. io.on("newImage", function(data) {
  139. var call, result;
  140. console.log(data);
  141. result = new Array();
  142. result.push(data.item);
  143. call = (config.front.moderation ? "toModerateList" : "unprintedList");
  144. io.sockets.emit(call, result);
  145. });
  146. io.on("reprint", function(id, count) {
  147. Image.find({
  148. _id: id
  149. }).exec(function(err, data) {
  150. var i;
  151. console.log("Reprinted:", id, count, data);
  152. if (err) {
  153. throw err;
  154. }
  155. i = 0;
  156. while (i < count) {
  157. io.sockets.emit("unprintedList", data);
  158. i++;
  159. }
  160. });
  161. });
  162. io.sockets.on("connection", function(socket) {
  163. console.log("Socket " + socket.id + " connected");
  164. socket.emit("config", config.front);
  165. socketIOstream(socket).on("image", function(stream, data) {
  166. var filename;
  167. console.log("Socket stream:", data);
  168. filename = path.basename(data.nname);
  169. stream.pipe(fs.createWriteStream(config.images.borPrefix + filename));
  170. });
  171. Image.find({
  172. isPrinted: false,
  173. moderationStatus: 2
  174. }).exec(function(err, data) {
  175. socket.emit("unprintedList", data);
  176. });
  177. Image.find({
  178. isPrinted: false,
  179. moderationStatus: {
  180. $nin: [1, 2]
  181. }
  182. }).exec(function(err, data) {
  183. socket.emit("toModerateList", data);
  184. });
  185. socket.on("newTag", function(data) {
  186. var sub;
  187. sub = new Subscription(data);
  188. sub.save(function(err, result) {
  189. if (err) {
  190. console.log(err);
  191. return;
  192. }
  193. io.sockets.emit("newTagAdded", result);
  194. });
  195. });
  196. socket.on("deleteTag", function(data) {
  197. console.log(data);
  198. return Subscription.findOne({
  199. sid: data
  200. }, function(err, sub) {
  201. console.log(err, sub);
  202. return sub.remove(function() {
  203. console.log(data);
  204. return io.sockets.emit("deletedTag", data);
  205. });
  206. });
  207. });
  208. socket.on("initTag", function(tag) {
  209. media(tag, function(limit) {
  210. io.sockets.emit("limitUpdate", limit);
  211. config.front.limit = limit;
  212. });
  213. });
  214. socket.on("clearDb", function() {
  215. var i;
  216. i = new Image();
  217. i.collection.drop(function(err) {
  218. if (err) {
  219. throw err;
  220. }
  221. console.log("collection dropped");
  222. io.sockets.emit("dbCleared");
  223. });
  224. });
  225. socket.on("filePrinted", function(id) {
  226. console.log("File Printed", id);
  227. Image.find({
  228. _id: id
  229. }).exec(function(err, data) {
  230. console.log("filePrinted");
  231. if (err) {
  232. throw err;
  233. }
  234. data[0].isPrinted = true;
  235. data[0].save(function(err) {
  236. if (err) {
  237. throw err;
  238. }
  239. io.sockets.emit("filePrinted", data[0]);
  240. });
  241. });
  242. });
  243. socket.on("fileApproved", function(id) {
  244. Image.findOneAndUpdate({
  245. _id: id
  246. }, {
  247. $set: {
  248. moderationStatus: 2
  249. }
  250. }, function(err, data) {
  251. var result;
  252. if (err) {
  253. throw err;
  254. }
  255. io.sockets.emit("fileApproved", id);
  256. result = new Array();
  257. result.push(data);
  258. io.sockets.emit("unprintedList", result);
  259. });
  260. });
  261. socket.on("fileDenied", function(id) {
  262. Image.findOneAndUpdate({
  263. _id: id
  264. }, {
  265. $set: {
  266. moderationStatus: 1
  267. }
  268. }, function(err, data) {
  269. if (err) {
  270. throw err;
  271. }
  272. io.sockets.emit("fileDenied", id);
  273. });
  274. });
  275. socket.on("changeModeration", function(val) {
  276. global.config.front.moderation = val;
  277. return config.front.moderation = val;
  278. });
  279. });
  280. io.on("error", function(err) {
  281. console.log("Caught flash policy server socket error: ", err.stack);
  282. });
  283. }).call(this);