Desktop markdown wiki app. Built with node, Electron Framework and AngularJS.

thumbnail-service.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. angular.module('codexApp')
  2. .service('ThumbnailService', [ '$rootScope', '$http', function($rootScope, $http) {
  3. var createThumbnail = function(file_path) {
  4. //console.log("-> Creating Thumbnail for " + file_path);
  5. var webshot = require('webshot');
  6. var fs = require('fs');
  7. var marked = require('marked');
  8. var options = {
  9. screenSize: {
  10. width: 220
  11. , height: 170
  12. }
  13. , shotSize: {
  14. width: 220
  15. , height: '170'
  16. }
  17. , userAgent: 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_2 like Mac OS X; en-us)'
  18. + ' AppleWebKit/531.21.20 (KHTML, like Gecko) Mobile/7B298g'
  19. , siteType:'html'
  20. };
  21. var data = fs.readFileSync(file_path);
  22. var page_data = String.fromCharCode.apply(null, data);
  23. var thumbnail_path = getThumbnailName(file_path);
  24. var page = '<html><head><style>body {width: 210px;} body, h1, h2, h3, h4, h5, p, span, table, code, ul, ol { font-family: helvetica; overflow-wrap: break-word; font-weight: 300; } p, ul, ol, code { font-size: 11px;} h1 {font-size: 16px;} h2 {font-size: 14px;} h3 { font-size: 12px; font-weight: 400} code { font-family: monospace; }</style></head><body>' + marked(page_data); + '</body></html>';
  25. var renderStream = webshot(page, thumbnail_path, options, function(err) {
  26. // screenshot now saved to hello_world.png
  27. });
  28. //var file = fs.createWriteStream('google.png', {encoding: 'binary'});
  29. console.log("-> Created thumbnail " + thumbnail_path);
  30. return thumbnail_path;
  31. }
  32. var getThumbnailName = function(file_path) {
  33. var filename = file_path.split('\\').pop().split('/').pop();
  34. var name = filename.split('.');
  35. //if (lastIndex < 1) return "";
  36. var path = file_path.split('/');
  37. path.pop();
  38. var thumb_path = path.join('/');
  39. return thumb_path + "/" + name[0] + ".thumb.png";
  40. }
  41. var thumbnailExists = function(file_path) {
  42. var url = getThumbnailName(file_path);
  43. //console.log(url);
  44. // var http = new XMLHttpRequest();
  45. // http.open('HEAD', url, false);
  46. // http.send();
  47. // console.log(http.status==200);
  48. // return http.status==200;
  49. var fs = require('fs');
  50. return fs.existsSync(url);
  51. }
  52. this.createNoteThumbnail = function(file_path) {
  53. if(thumbnailExists(file_path) == false){
  54. return createThumbnail(file_path);
  55. }
  56. return getThumbnailName(file_path);
  57. }
  58. }])