utils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var exec = require('child_process').exec
  2. , fs = require('fs')
  3. , path = require('path');
  4. var errors = require('./errors');
  5. /**
  6. * Exec the list of commands and call the callback function at the end of the process
  7. */
  8. module.exports.exec = function (commands, settings, callback) {
  9. // Create final command line
  10. var finalCommand = commands.join(" ");
  11. // Create the timeoutId for stop the timeout at the end the process
  12. var timeoutID = null;
  13. // Exec the command
  14. var process = exec(finalCommand, settings, function (error, stdout, stderr) {
  15. // Clear timeout if 'timeoutID' are setted
  16. if (timeoutID !== null) clearTimeout(timeoutID);
  17. // Call the callback function
  18. callback(error, stdout, stderr);
  19. });
  20. // Verify if the timeout are setting
  21. if (settings.timeout > 0) {
  22. // Set the timeout
  23. timeoutID = setTimeout(function () {
  24. process.kill();
  25. }, 100);
  26. }
  27. }
  28. /**
  29. * Check if object is empty
  30. */
  31. module.exports.isEmptyObj = function (obj) {
  32. // Scan all properties
  33. for(var prop in obj)
  34. // Check if obj has a property
  35. if(obj.hasOwnProperty(prop))
  36. // The object is not empty
  37. return false;
  38. // The object is empty
  39. return true;
  40. }
  41. /**
  42. * Merge obj1 into obj
  43. */
  44. module.exports.mergeObject = function (obj, obj1) {
  45. // Check if there are options set
  46. if (!module.exports.isEmptyObj(obj1)) {
  47. // Scan all settings
  48. for (var key in obj1) {
  49. // Check if the option is valid
  50. if (!obj.hasOwnProperty(key))
  51. throw errors.renderError('invalid_option_name', key);
  52. // Set new option value
  53. obj[key] = obj1[key];
  54. }
  55. }
  56. }
  57. /**
  58. * Calculate the duration in seconds from the string retrieved by the ffmpeg info
  59. */
  60. module.exports.durationToSeconds = function(duration) {
  61. var parts = duration.substr(0,8).split(':');
  62. return parseInt(parts[0], 10) * 3600 + parseInt(parts[1], 10) * 60 + parseInt(parts[2], 10);
  63. };
  64. /**
  65. * Calculate the greatest common divisor
  66. */
  67. module.exports.gcd = function (a, b) {
  68. if (b === 0) return a;
  69. return module.exports.gcd(b, a % b);
  70. }
  71. /**
  72. * Offers functionality similar to mkdir -p
  73. */
  74. module.exports.mkdir = function (dirpath, mode, callback, position) {
  75. // Split all directories
  76. var parts = path.normalize(dirpath).split('/');
  77. // If the first part is empty then remove this part
  78. if (parts[0] == "")
  79. parts = parts.slice(1);
  80. // Set the initial configuration
  81. mode = mode || 0777;
  82. position = position || 0;
  83. // Check se current position is greater than the list of folders
  84. if (position > parts.length) {
  85. // If isset the callback then it will be invoked
  86. if (callback)
  87. callback();
  88. // Exit and return a positive value
  89. return true;
  90. }
  91. // Build the directory path
  92. var directory = (dirpath.charAt(0) == '/' ? '/' : '') + parts.slice(0, position + 1).join('/');
  93. // Check if directory exists
  94. if (fs.existsSync(directory)) {
  95. module.exports.mkdir(dirpath, mode, callback, position + 1);
  96. } else {
  97. if (fs.mkdirSync(directory, mode)) {
  98. // If isset the callback then it will be invoked
  99. if (callback)
  100. callback(errors.renderError('mkdir', directory));
  101. // Send the new exception
  102. throw errors.renderError('mkdir', directory);
  103. } else {
  104. module.exports.mkdir(dirpath, mode, callback, position + 1);
  105. }
  106. }
  107. }
  108. /**
  109. * Check if a value is present inside an array
  110. */
  111. module.exports.in_array = function (value, array) {
  112. // Scan all element
  113. for (var i in array)
  114. // Check if value exists
  115. if (array[i] == value)
  116. // Return the position of value
  117. return i;
  118. // The value not exists
  119. return false;
  120. }