file_slideshow.module

Tracking 6.x-2.x branch
  1. drupal
    1. 6 contributions/fileframework/formats/slideshow/file_slideshow.module

Supports slideshow and presentation file formats.

Functions & methods

NameDescription
file_slideshow_menuImplementation of hook_menu().
file_slideshow_metadata_infoImplementation of hook_metadata_info().
file_slideshow_metadata_parseImplementation of hook_metadata_parse().
file_slideshow_mime_convertersImplementation of hook_mime_converters().
file_slideshow_mime_handlersImplementation of hook_mime_handlers().
file_slideshow_mime_typesImplementation of hook_mime_types().
file_slideshow_pdf_generateGenerates a slideshow_pdf preview.
file_slideshow_pdf_secondaryslideshow_pdf secondary previews.
file_slideshow_rdf_namespacesImplementation of hook_rdf_namespaces().
file_slideshow_themeImplementation of hook_theme().
_file_slideshow_pdfPDF info to RDF mapping.

Constants

NameDescription
FILE_SLIDESHOW_PDF

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Supports slideshow and presentation file formats.
  5. */
  6. //////////////////////////////////////////////////////////////////////////////
  7. define('FILE_SLIDESHOW_PDF', variable_get('file_slideshow_pdf', 1));
  8. //////////////////////////////////////////////////////////////////////////////
  9. // Core API hooks
  10. /**
  11. * Implementation of hook_theme().
  12. */
  13. function file_slideshow_theme() {
  14. return array(
  15. 'file_slideshow_admin_settings' => array(
  16. 'arguments' => array('form' => NULL),
  17. 'file' => 'file_slideshow.theme.inc'
  18. ),
  19. );
  20. }
  21. /**
  22. * Implementation of hook_menu().
  23. */
  24. function file_slideshow_menu() {
  25. return array(
  26. 'admin/settings/file/format/slideshow' => array(
  27. 'title' => 'Slideshow',
  28. 'description' => 'Manage the slideshow files.',
  29. 'page callback' => 'drupal_get_form',
  30. 'page arguments' => array('file_slideshow_admin_settings'),
  31. 'access arguments' => array('administer site configuration'),
  32. 'file' => 'file_slideshow.admin.inc',
  33. ),
  34. );
  35. }
  36. //////////////////////////////////////////////////////////////////////////////
  37. // File API hooks
  38. /**
  39. * Implementation of hook_mime_handlers().
  40. */
  41. function file_slideshow_mime_handlers() {
  42. return array(
  43. 'file_slideshow_pdf' => array(
  44. 'name' => t('PDF'),
  45. ),
  46. );
  47. }
  48. /**
  49. * Implementation of hook_mime_types().
  50. */
  51. function file_slideshow_mime_types() {
  52. return array(
  53. 'application/pdf' => array(
  54. 'name' => t('PDF document'),
  55. 'icon' => 'acrobat.gif',
  56. ),
  57. 'application/postscript' => array(
  58. 'name' => t('PostScript document'),
  59. 'icon' => 'ps.gif',
  60. ),
  61. 'application/vnd.oasis.opendocument.presentation' => array(
  62. 'name' => t('OpenDocument Presentation'),
  63. 'icon' => 'oo-draw.gif',
  64. ),
  65. 'application/vnd.ms-powerpoint' => array(
  66. 'name' => t('PowerPoint presentation'),
  67. 'icon' => 'powerpoint.gif',
  68. ),
  69. 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => array(
  70. 'name' => t('PowerPoint presentation'),
  71. 'icon' => 'powerpoint.gif',
  72. ),
  73. 'application/vnd.ms-powerpoint.presentation.macroEnabled.12' => array(
  74. 'name' => t('PowerPoint presentation'),
  75. 'icon' => 'powerpoint.gif',
  76. ),
  77. );
  78. }
  79. /**
  80. * Implementation of hook_mime_converters().
  81. */
  82. function file_slideshow_mime_converters() {
  83. return array(
  84. 'application/postscript' => array(
  85. 'text/plain' => array(
  86. 'pipeline' => '{pstotext} -output "[out_file]" "[in_file]"',
  87. 'handlers' => array('file_text_text'),
  88. ),
  89. ),
  90. 'application/pdf' => array(
  91. 'text/plain' => array(
  92. 'pipeline' => '{pdftotext} "[in_file]" "[out_file]"',
  93. 'handlers' => array('file_text_text'),
  94. ),
  95. 'image/jpeg' => array(
  96. 'pipeline' => '{gs} -q -sDEVICE=jpeg -dBATCH -dNOPAUSE -dFirstPage=1 -dLastPage=1 -r300 -sOutputFile="[out_file]" "[in_file]"',
  97. //'pipeline' => '{mv} "[in_file]" "[in_file].pdf"; {convert} "[in_file].pdf"\[0\] "[out_file].jpeg"; {mv} "[in_file].pdf" "[in_file]"; {mv} "[out_file].jpeg" "[out_file]"',
  98. 'handlers' => array('file_image_image'),
  99. ),
  100. 'application/x-shockwave-flash' => array(
  101. 'pipeline' => 'cd "[tmpdir]"; {pdf2swf} -lb "[in_file_base]" "[out_file_base]"',
  102. 'handlers' => array('file_video_flash_swf'),
  103. ),
  104. 'application/zip' => array(
  105. 'pipeline' => 'pwd="`{pwd}`"; {mkdir} "[in_file].tmp"; {mv} "[in_file]" "[in_file].tmp/[filename]"; cd "[in_file].tmp"; {gzip} -c "[filename]" > "file.zip"; cd "$pwd"; mv "[in_file].tmp/file.zip" "[out_file]"; {touch} "[in_file]"; {rm} -rf "[in_file].tmp"',
  106. ),
  107. ),
  108. 'application/vnd.oasis.opendocument.presentation' => array(
  109. 'application/pdf' => array(
  110. 'pipeline' => '{localhost:8100}{mv} "[in_file]" "[in_file].odp"; {java} -jar {{/opt/jodconverter/lib/jodconverter-cli.jar}} -f pdf "[in_file].odp"; {mv} "[in_file].odp" "[in_file]"; {mv} "[in_file].pdf" "[out_file]"',
  111. 'handlers' => array('file_slideshow_pdf'),
  112. ),
  113. 'application/vnd.ms-powerpoint' => array(
  114. 'pipeline' => '{localhost:8100}{mv} "[in_file]" "[in_file].odp"; {java} -jar {{/opt/jodconverter/lib/jodconverter-cli.jar}} -f ppt "[in_file].odp"; {mv} "[in_file].odp" "[in_file]"; {mv} "[in_file].ppt" "[out_file]"',
  115. ),
  116. 'application/zip' => array(
  117. 'pipeline' => 'pwd="`{pwd}`"; {mkdir} "[in_file].tmp"; {mv} "[in_file]" "[in_file].tmp/[filename]"; cd "[in_file].tmp"; {gzip} -c "[filename]" > "file.zip"; cd "$pwd"; mv "[in_file].tmp/file.zip" "[out_file]"; {touch} "[in_file]"; {rm} -rf "[in_file].tmp"',
  118. ),
  119. ),
  120. 'application/vnd.ms-powerpoint' => array(
  121. 'application/pdf' => array(
  122. 'pipeline' => '{localhost:8100}{mv} "[in_file]" "[in_file].ppt"; {java} -jar {{/opt/jodconverter/lib/jodconverter-cli.jar}} -f pdf "[in_file].ppt"; {mv} "[in_file].ppt" "[in_file]"; {mv} "[in_file].pdf" "[out_file]"',
  123. 'handlers' => array('file_slideshow_pdf'),
  124. ),
  125. 'application/vnd.oasis.opendocument.presentation' => array(
  126. 'pipeline' => '{localhost:8100}{mv} "[in_file]" "[in_file].ppt"; {java} -jar {{/opt/jodconverter/lib/jodconverter-cli.jar}} -f odp "[in_file].ppt"; {mv} "[in_file].ppt" "[in_file]"; {mv} "[in_file].odp" "[out_file]"',
  127. ),
  128. 'application/zip' => array(
  129. 'pipeline' => 'pwd="`{pwd}`"; {mkdir} "[in_file].tmp"; {mv} "[in_file]" "[in_file].tmp/[filename]"; cd "[in_file].tmp"; {gzip} -c "[filename]" > "file.zip"; cd "$pwd"; mv "[in_file].tmp/file.zip" "[out_file]"; {touch} "[in_file]"; {rm} -rf "[in_file].tmp"',
  130. ),
  131. ),
  132. 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => array(
  133. 'application/pdf' => array(
  134. 'pipeline' => '{localhost:8100}{mv} "[in_file]" "[in_file].pptx"; {java} -jar {{/opt/jodconverter/lib/jodconverter-cli.jar}} -f pdf "[in_file].pptx"; {mv} "[in_file].pptx" "[in_file]"; {mv} "[in_file].pdf" "[out_file]"',
  135. 'handlers' => array('file_slideshow_pdf'),
  136. ),
  137. 'application/vnd.oasis.opendocument.presentation' => array(
  138. 'pipeline' => '{localhost:8100}{mv} "[in_file]" "[in_file].pptx"; {java} -jar {{/opt/jodconverter/lib/jodconverter-cli.jar}} -f odp "[in_file].pptx"; {mv} "[in_file].pptx" "[in_file]"; {mv} "[in_file].odp" "[out_file]"',
  139. ),
  140. 'application/vnd.ms-powerpoint' => array(
  141. 'pipeline' => '{localhost:8100}{mv} "[in_file]" "[in_file].pptx"; {java} -jar {{/opt/jodconverter/lib/jodconverter-cli.jar}} -f ppt "[in_file].pptx"; {mv} "[in_file].pptx" "[in_file]"; {mv} "[in_file].ppt" "[out_file]"',
  142. ),
  143. 'application/zip' => array(
  144. 'pipeline' => 'pwd="`{pwd}`"; {mkdir} "[in_file].tmp"; {mv} "[in_file]" "[in_file].tmp/[filename]"; cd "[in_file].tmp"; {gzip} -c "[filename]" > "file.zip"; cd "$pwd"; mv "[in_file].tmp/file.zip" "[out_file]"; {touch} "[in_file]"; {rm} -rf "[in_file].tmp"',
  145. ),
  146. ),
  147. 'application/vnd.ms-powerpoint.presentation.macroEnabled.12' => array(
  148. 'application/pdf' => array(
  149. 'pipeline' => '{localhost:8100}{mv} "[in_file]" "[in_file].pptx"; {java} -jar {{/opt/jodconverter/lib/jodconverter-cli.jar}} -f pdf "[in_file].pptx"; {mv} "[in_file].pptx" "[in_file]"; {mv} "[in_file].pdf" "[out_file]"',
  150. 'handlers' => array('file_slideshow_pdf'),
  151. ),
  152. 'application/vnd.oasis.opendocument.presentation' => array(
  153. 'pipeline' => '{localhost:8100}{mv} "[in_file]" "[in_file].pptx"; {java} -jar {{/opt/jodconverter/lib/jodconverter-cli.jar}} -f odp "[in_file].pptx"; {mv} "[in_file].pptx" "[in_file]"; {mv} "[in_file].odp" "[out_file]"',
  154. ),
  155. 'application/vnd.ms-powerpoint' => array(
  156. 'pipeline' => '{localhost:8100}{mv} "[in_file]" "[in_file].pptx"; {java} -jar {{/opt/jodconverter/lib/jodconverter-cli.jar}} -f ppt "[in_file].pptx"; {mv} "[in_file].pptx" "[in_file]"; {mv} "[in_file].ppt" "[out_file]"',
  157. ),
  158. 'application/zip' => array(
  159. 'pipeline' => 'pwd="`{pwd}`"; {mkdir} "[in_file].tmp"; {mv} "[in_file]" "[in_file].tmp/[filename]"; cd "[in_file].tmp"; {gzip} -c "[filename]" > "file.zip"; cd "$pwd"; mv "[in_file].tmp/file.zip" "[out_file]"; {touch} "[in_file]"; {rm} -rf "[in_file].tmp"',
  160. ),
  161. ),
  162. );
  163. }
  164. /**
  165. * Implementation of hook_metadata_info().
  166. */
  167. function file_slideshow_metadata_info() {
  168. $info = array();
  169. foreach (_file_slideshow_pdf() as $name => $data) {
  170. $info[$data['rdf']] = array('name' => t($data['name']));
  171. if (isset($data['theme']))
  172. $info[$data['rdf']]['theme'] = $data['theme'];
  173. }
  174. return $info;
  175. }
  176. /**
  177. * Implementation of hook_metadata_parse().
  178. */
  179. function file_slideshow_metadata_parse($filename, $mimetype) {
  180. if ($mimetype != 'application/pdf')
  181. return NULL;
  182. if (!($pdfinfo = _file_command_exists('pdfinfo')))
  183. return NULL;
  184. $metadata = array();
  185. $pdf = _file_slideshow_pdf();
  186. foreach ($pdf as $name => $data) {
  187. $pdf_data_default[$name] = $data['default'];
  188. }
  189. $pdf_data = variable_get('file_slideshow_pdf_data', $pdf_data_default);
  190. if (_file_command_run('pdfinfo '. escapeshellarg($filename), $pipes) === 0 && !empty($pipes['stdout'])) {
  191. $info = array();
  192. foreach (explode("\n", $pipes['stdout']) as $line) {
  193. if (preg_match('/^([^:]+):(.*)$/', $line, $matches))
  194. $info[trim($matches[1])] = trim($matches[2]);
  195. }
  196. $rdf_pdf = array();
  197. foreach ($pdf as $name => $data) {
  198. if (isset($pdf_data[$name]) && $element = file_get_recursive($info, $data['pdf'])) {
  199. switch ($data['type']) {
  200. case 'int' :
  201. $rdf_pdf[$data['rdf']] = array((int)$element);
  202. break;
  203. default :
  204. $rdf_pdf[$data['rdf']] = array($element);
  205. break;
  206. }
  207. }
  208. }
  209. $metadata = array_merge($rdf_pdf, $metadata);
  210. }
  211. return $metadata;
  212. }
  213. //////////////////////////////////////////////////////////////////////////////
  214. // MIME handlers
  215. /**
  216. * Generates a slideshow_pdf preview.
  217. */
  218. function file_slideshow_pdf_generate($file, $tier = NULL) {
  219. $source = TRUE;
  220. if (isset($tier) && !in_array($file->handlers[$tier], isset($file->previews[$file->mimes[$tier]]) ? $file->previews[$file->mimes[$tier]] : array())) {
  221. if (!file_get_converted($file, $tier) || !file_data_save($file, $tier))
  222. $source = FALSE;
  223. else
  224. watchdog('file_slideshow', 'The %mime preview was generated for the file %name, uri=%uri.', array('%mime' => $file->mimes[$tier], '%name' => $file->filename, '%uri' => $file->uri), WATCHDOG_NOTICE, isset($file->nid) ? l(t('view'), 'node/'. $file->nid) : NULL);
  225. }
  226. // Create a plain text, image and swf previews.
  227. $result = TRUE;
  228. $tier = isset($tier) ? $tier + 1 : 0;
  229. foreach (file_slideshow_pdf_secondary() as $handler => $to) {
  230. if ($source) {
  231. $file->handlers[$tier] = $handler;
  232. $file->mimes[$tier] = $to;
  233. $result = file_generate_secondary_previews($file, $tier) && $result;
  234. }
  235. else {
  236. file_mark_failed($file, $to, $handler);
  237. }
  238. }
  239. return $result;
  240. }
  241. /**
  242. * slideshow_pdf secondary previews.
  243. */
  244. function file_slideshow_pdf_secondary() {
  245. return array(
  246. 'file_text_text' => 'text/plain',
  247. 'file_image_image' => 'image/jpeg',
  248. 'file_video_flash_swf' => 'application/x-shockwave-flash',
  249. );
  250. }
  251. //////////////////////////////////////////////////////////////////////////////
  252. // RDF API hooks
  253. /**
  254. * Implementation of hook_rdf_namespaces().
  255. */
  256. function file_slideshow_rdf_namespaces() {
  257. return array(
  258. // 'wordnet' => 'http://xmlns.com/wordnet/1.6/',
  259. );
  260. }
  261. //////////////////////////////////////////////////////////////////////////////
  262. // PDF info settings
  263. /**
  264. * PDF info to RDF mapping.
  265. */
  266. function _file_slideshow_pdf() {
  267. return array(
  268. 'title' => array('name' => 'Title', 'pdf' => array('Title' => NULL), 'rdf' => 'wordnet:title', 'type' => '', 'default' => 1),
  269. 'subject' => array('name' => 'Subject', 'pdf' => array('Subject' => NULL), 'rdf' => 'wordnet:subject', 'type' => '', 'default' => 1),
  270. 'author' => array('name' => 'Author', 'pdf' => array('Author' => NULL), 'rdf' => 'wordnet:author', 'type' => '', 'default' => 1),
  271. 'creator' => array('name' => 'Creator', 'pdf' => array('Creator' => NULL), 'rdf' => 'wordnet:creator', 'type' => '', 'default' => 1),
  272. 'producer' => array('name' => 'Producer', 'pdf' => array('Producer' => NULL), 'rdf' => 'wordnet:producer', 'type' => '', 'default' => 1),
  273. 'creationdate' => array('name' => 'Creation Date', 'pdf' => array('CreationDate' => NULL), 'rdf' => 'wordnet:creation_date', 'type' => '', 'default' => 1),
  274. 'moddate' => array('name' => 'Modify Date', 'pdf' => array('ModDate' => NULL), 'rdf' => 'wordnet:mod_date', 'type' => '', 'default' => 1),
  275. 'tagged' => array('name' => 'Tagged', 'pdf' => array('Tagged' => NULL), 'rdf' => 'wordnet:tagged', 'type' => '', 'default' => 1),
  276. 'pages' => array('name' => 'Pages', 'pdf' => array('Pages' => NULL), 'rdf' => 'wordnet:pages', 'type' => 'int', 'default' => 1),
  277. 'encrypted' => array('name' => 'Encrypted', 'pdf' => array('Encrypted' => NULL), 'rdf' => 'wordnet:encrypted', 'type' => '', 'default' => 1),
  278. 'pagesize' => array('name' => 'Page size', 'pdf' => array('Page size' => NULL), 'rdf' => 'wordnet:page_size', 'type' => '', 'default' => 1),
  279. 'filesize' => array('name' => 'File size', 'pdf' => array('File size' => NULL), 'rdf' => 'wordnet:file_size', 'type' => '', 'default' => 1),
  280. 'optimized' => array('name' => 'Optimized', 'pdf' => array('Optimized' => NULL), 'rdf' => 'wordnet:optimized', 'type' => '', 'default' => 1),
  281. 'pdfversion' => array('name' => 'PDF version', 'pdf' => array('PDF version' => NULL), 'rdf' => 'wordnet:pdf_version', 'type' => '', 'default' => 1),
  282. );
  283. }