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

thumbnail-service.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. angular.module('codexApp')
  2. .service('ThumbnailService', [ '$rootScope', '$http', function($rootScope, $http) {
  3. var createThumbnail = function(file_path, app_data_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. console.log(app_data_path)
  24. var thumbnail_path = getThumbnailName(file_path, app_data_path);
  25. 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>';
  26. var renderStream = webshot(page, thumbnail_path, options, function(err) {
  27. // screenshot now saved to hello_world.png
  28. });
  29. //var file = fs.createWriteStream('google.png', {encoding: 'binary'});
  30. console.log("-> Created thumbnail " + thumbnail_path);
  31. return thumbnail_path;
  32. }
  33. var getThumbnailName = function(file_path, app_data_path) {
  34. var filename = file_path.split('\\').pop().split('/').pop();
  35. var name = filename.split('.');
  36. //if (lastIndex < 1) return "";
  37. var path = file_path.split('/');
  38. path.pop();
  39. var thumb_path = path.join('/');
  40. //return thumb_path + "/" + name[0] + ".thumb.png";
  41. var date = new Date();
  42. var components = [
  43. date.getYear(),
  44. date.getMonth(),
  45. date.getDate(),
  46. date.getHours(),
  47. date.getMinutes(),
  48. date.getSeconds(),
  49. date.getMilliseconds()
  50. ];
  51. var id = components.join("");
  52. return app_data_path + "/thumbs/" + id + ".png"
  53. }
  54. var thumbnailExists = function(file_path) {
  55. var url = getThumbnailName(file_path);
  56. //console.log(url);
  57. // var http = new XMLHttpRequest();
  58. // http.open('HEAD', url, false);
  59. // http.send();
  60. // console.log(http.status==200);
  61. // return http.status==200;
  62. var fs = require('fs');
  63. return fs.existsSync(url);
  64. }
  65. this.createNoteThumbnail = function(file_path, app_data_path) {
  66. if(thumbnailExists(file_path) == false){
  67. return createThumbnail(file_path, app_data_path);
  68. }
  69. return getThumbnailName(file_path);
  70. }
  71. }])