/** * Download (accessible) media for all records in this set */ public function getSetMedia() { set_time_limit(600); // allow a lot of time for this because the sets can be potentially large $o_dm = Datamodel::load(); $t_set = new ca_sets($this->request->getParameter('set_id', pInteger)); if (!$t_set->getPrimaryKey()) { $this->notification->addNotification(_t('No set defined'), __NOTIFICATION_TYPE_ERROR__); $this->opo_response->setRedirect(caEditorUrl($this->opo_request, 'ca_sets', $t_set->getPrimaryKey())); return false; } $va_record_ids = array_keys($t_set->getItemRowIDs(array('limit' => 100000))); if (!is_array($va_record_ids) || !sizeof($va_record_ids)) { $this->notification->addNotification(_t('No media is available for download'), __NOTIFICATION_TYPE_ERROR__); $this->opo_response->setRedirect(caEditorUrl($this->opo_request, 'ca_sets', $t_set->getPrimaryKey())); return false; } $vs_subject_table = $o_dm->getTableName($t_set->get('table_num')); $t_instance = $o_dm->getInstanceByTableName($vs_subject_table); $qr_res = $vs_subject_table::createResultSet($va_record_ids); $qr_res->filterNonPrimaryRepresentations(false); $va_paths = array(); while ($qr_res->nextHit()) { $va_original_paths = $qr_res->getMediaPaths('ca_object_representations.media', 'original'); if (sizeof($va_original_paths) > 0) { $va_paths[$qr_res->get($t_instance->primaryKey())] = array('idno' => $qr_res->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD')), 'paths' => $va_original_paths); } } if (sizeof($va_paths) > 0) { $vs_tmp_name = caGetTempFileName('DownloadSetMedia', 'zip'); $o_phar = new PharData($vs_tmp_name, null, null, Phar::ZIP); foreach ($va_paths as $vn_pk => $va_path_info) { $vn_c = 1; foreach ($va_path_info['paths'] as $vs_path) { if (!file_exists($vs_path)) { continue; } $vs_filename = $va_path_info['idno'] ? $va_path_info['idno'] : $vn_pk; $vs_filename .= "_{$vn_c}"; if ($vs_ext = pathinfo($vs_path, PATHINFO_EXTENSION)) { $vs_filename .= ".{$vs_ext}"; } $o_phar->addFile($vs_path, $vs_filename); $vn_c++; } } $o_view = new View($this->request, $this->request->getViewsDirectoryPath() . '/bundles/'); // send download $vs_set_code = $t_set->get('set_code'); $o_view->setVar('tmp_file', $vs_tmp_name); $o_view->setVar('download_name', 'media_for_' . mb_substr(preg_replace('![^A-Za-z0-9]+!u', '_', $vs_set_code ? $vs_set_code : $t_set->getPrimaryKey()), 0, 20) . '.zip'); $this->response->addContent($o_view->render('ca_sets_download_media.php')); return; } else { $this->notification->addNotification(_t('No media is available for download'), __NOTIFICATION_TYPE_ERROR__); $this->opo_response->setRedirect(caEditorUrl($this->opo_request, 'ca_sets', $t_set->getPrimaryKey())); return; } return $this->Edit(); }
function caZipDirectory($ps_directory, $ps_name, $ps_output_file) { $va_files_to_zip = caGetDirectoryContentsAsList($ps_directory); $vs_tmp_name = caGetTempFileName('caZipDirectory', 'zip'); $o_phar = new PharData($vs_tmp_name, null, null, Phar::ZIP); foreach ($va_files_to_zip as $vs_file) { $vs_name = str_replace($ps_directory, $ps_name, $vs_file); $o_phar->addFile($vs_file, $vs_name); } copy($vs_tmp_name, $ps_output_file); unlink($vs_tmp_name); return true; }
/** * Download all media attached to specified object (not necessarily open for editing) * Includes all representation media attached to the specified object + any media attached to oter * objects in the same object hierarchy as the specified object. Used by the book viewer interfacce to * initiate a download. */ public function DownloadMedia($pa_options = null) { list($vn_subject_id, $t_subject) = $this->_initView(); $pn_representation_id = $this->request->getParameter('representation_id', pInteger); $pn_value_id = $this->request->getParameter('value_id', pInteger); if ($pn_value_id) { return $this->DownloadAttributeMedia(); } $ps_version = $this->request->getParameter('version', pString); if (!$vn_subject_id) { return; } $o_view = new View($this->request, $this->request->getViewsDirectoryPath() . '/bundles/'); if (!$ps_version) { $ps_version = 'original'; } $o_view->setVar('version', $ps_version); $va_ancestor_ids = $t_subject->isHierarchical() ? $t_subject->getHierarchyAncestors(null, array('idsOnly' => true, 'includeSelf' => true)) : array($vn_subject_id); if ($vn_parent_id = array_pop($va_ancestor_ids)) { $t_subject->load($vn_parent_id); array_unshift($va_ancestor_ids, $vn_parent_id); } $va_child_ids = $t_subject->isHierarchical() ? $t_subject->getHierarchyChildren(null, array('idsOnly' => true)) : array($vn_subject_id); foreach ($va_ancestor_ids as $vn_id) { array_unshift($va_child_ids, $vn_id); } $vn_c = 1; $va_file_names = array(); $va_file_paths = array(); foreach ($va_child_ids as $vn_child_id) { if (!$t_subject->load($vn_child_id)) { continue; } if ($t_subject->tableName() == 'ca_object_representations') { $va_reps = array($vn_child_id => array('representation_id' => $vn_child_id, 'info' => array($ps_version => $t_subject->getMediaInfo('media', $ps_version)))); } else { $va_reps = $t_subject->getRepresentations(array($ps_version)); } $vs_idno = $t_subject->get('idno'); foreach ($va_reps as $vn_representation_id => $va_rep) { if ($pn_representation_id && $pn_representation_id != $vn_representation_id) { continue; } $va_rep_info = $va_rep['info'][$ps_version]; $vs_idno_proc = preg_replace('![^A-Za-z0-9_\\-]+!', '_', $vs_idno); switch ($this->request->user->getPreference('downloaded_file_naming')) { case 'idno': $vs_file_name = $vs_idno_proc . '_' . $vn_c . '.' . $va_rep_info['EXTENSION']; break; case 'idno_and_version': $vs_file_name = $vs_idno_proc . '_' . $ps_version . '_' . $vn_c . '.' . $va_rep_info['EXTENSION']; break; case 'idno_and_rep_id_and_version': $vs_file_name = $vs_idno_proc . '_representation_' . $vn_representation_id . '_' . $ps_version . '.' . $va_rep_info['EXTENSION']; break; case 'original_name': default: if ($va_rep['info']['original_filename']) { $va_tmp = explode('.', $va_rep['info']['original_filename']); if (sizeof($va_tmp) > 1) { if (strlen($vs_ext = array_pop($va_tmp)) < 3) { $va_tmp[] = $vs_ext; } } $vs_file_name = join('_', $va_tmp); } else { $vs_file_name = $vs_idno_proc . '_representation_' . $vn_representation_id . '_' . $ps_version; } if (isset($va_file_names[$vs_file_name . '.' . $va_rep_info['EXTENSION']])) { $vs_file_name .= "_{$vn_c}"; } $vs_file_name .= '.' . $va_rep_info['EXTENSION']; break; } $va_file_names[$vs_file_name] = true; $o_view->setVar('version_download_name', $vs_file_name); // // Perform metadata embedding $t_rep = new ca_object_representations($va_rep['representation_id']); if (!($vs_path = caEmbedMetadataIntoRepresentation($t_subject, $t_rep, $ps_version))) { $vs_path = $t_rep->getMediaPath('media', $ps_version); } $va_file_paths[$vs_path] = $vs_file_name; $vn_c++; } } if (sizeof($va_file_paths) > 1) { if (!($vn_limit = ini_get('max_execution_time'))) { $vn_limit = 30; } set_time_limit($vn_limit * 2); $vs_tmp_name = caGetTempFileName('DownloadMedia', 'zip'); $o_phar = new PharData($vs_tmp_name, null, null, Phar::ZIP); foreach ($va_file_paths as $vs_path => $vs_name) { $o_phar->addFile($vs_path, $vs_name); } $o_view->setVar('archive_path', $vs_tmp_name); $o_view->setVar('archive_name', preg_replace('![^A-Za-z0-9\\.\\-]+!', '_', $t_subject->get('idno')) . '.zip'); $this->response->addContent($o_view->render('download_media_binary.php')); if ($vs_tmp_name) { @unlink($vs_tmp_name); } } else { foreach ($va_file_paths as $vs_path => $vs_name) { $o_view->setVar('archive_path', $vs_path); $o_view->setVar('archive_name', $vs_name); } $this->response->addContent($o_view->render('download_media_binary.php')); } }
/** * Embed media metadata into given file. Embedding is performed on a copy of the file and placed into the * system tmp directory. The given file is never modified. * * @param string $ps_file The file to embed metadata into * @param string $ps_table Table name of the subject record. This is used to figure out the appropriate mapping to use from media_metadata.conf * @param int $pn_pk Primary key of the subject record. This is used to run the export for the right record. * @param string $ps_type_code Optional type code for the subject record * @param int $pn_rep_pk Primary key of the subject representation. * If there are export mapping for object representations, we run them after the mapping for the subject table. * Fields that get exported here should overwrite fields from the subject table export. * @param string $ps_rep_type_code type code for object representation * @return string File name of a temporary file with the embedded metadata, false on failure */ function caEmbedMediaMetadataIntoFile($ps_file, $ps_table, $pn_pk, $ps_type_code, $pn_rep_pk, $ps_rep_type_code) { require_once __CA_MODELS_DIR__ . '/ca_data_exporters.php'; if (!caExifToolInstalled()) { return false; } // we need exiftool for embedding $vs_path_to_exif_tool = caGetExternalApplicationPath('exiftool'); if (!file_exists($ps_file)) { return false; } if (!preg_match("/^image\\//", mime_content_type($ps_file))) { return false; } // Don't try to embed in files other than images // make a temporary copy (we won't touch the original) copy($ps_file, $vs_tmp_filepath = caGetTempDirPath() . "/" . time() . md5($ps_file)); // // SUBJECT TABLE // if ($vs_subject_table_export = caExportMediaMetadataForRecord($ps_table, $ps_type_code, $pn_pk)) { $vs_export_filename = caGetTempFileName('mediaMetadataSubjExport', 'xml'); if (@file_put_contents($vs_export_filename, $vs_subject_table_export) === false) { return false; } exec("{$vs_path_to_exif_tool} -tagsfromfile {$vs_export_filename} -all:all " . caEscapeShellArg($vs_tmp_filepath), $va_output, $vn_return); @unlink($vs_export_filename); @unlink("{$vs_tmp_filepath}_original"); } // // REPRESENTATION // if ($vs_representation_Export = caExportMediaMetadataForRecord('ca_object_representations', $ps_rep_type_code, $pn_rep_pk)) { $vs_export_filename = caGetTempFileName('mediaMetadataRepExport', 'xml'); if (@file_put_contents($vs_export_filename, $vs_representation_Export) === false) { return false; } exec("{$vs_path_to_exif_tool} -tagsfromfile {$vs_export_filename} -all:all " . caEscapeShellArg($vs_tmp_filepath), $va_output, $vn_return); @unlink($vs_export_filename); @unlink("{$vs_tmp_filepath}_original"); } return $vs_tmp_filepath; }
public function getLotMedia() { //if ((bool)$this->request->config->get('allow_download_of_all_object_media_in_a_lot')) { set_time_limit(600); // allow a lot of time for this because the sets can be potentially large $t_lot = new ca_object_lots($this->request->getParameter('lot_id', pInteger)); if ($t_lot->getPrimaryKey()) { $va_object_ids = $t_lot->get('ca_objects.object_id', array('returnAsArray' => true, 'limit' => 100000)); if (!is_array($va_object_ids) || !sizeof($va_object_ids)) { $this->notification->addNotification(_t('No media is available for download'), __NOTIFICATION_TYPE_ERROR__); $this->opo_response->setRedirect(caEditorUrl($this->opo_request, 'ca_object_lots', $t_lot->getPrimaryKey())); return; } $qr_res = ca_objects::createResultSet($va_object_ids); $qr_res->filterNonPrimaryRepresentations(false); $va_paths = array(); while ($qr_res->nextHit()) { $va_original_paths = $qr_res->getMediaPaths('ca_object_representations.media', 'original'); if (sizeof($va_original_paths) > 0) { $va_paths[$qr_res->get('object_id')] = array('idno' => $qr_res->get('idno'), 'paths' => $va_original_paths); } } if (sizeof($va_paths) > 0) { $vs_tmp_name = caGetTempFileName('DownloadLotMedia', 'zip'); $o_phar = new PharData($vs_tmp_name, null, null, Phar::ZIP); foreach ($va_paths as $vn_object_id => $va_path_info) { $vn_c = 1; foreach ($va_path_info['paths'] as $vs_path) { if (!file_exists($vs_path)) { continue; } $vs_filename = $va_path_info['idno'] ? $va_path_info['idno'] : $vn_object_id; $vs_filename .= "_{$vn_c}"; if ($vs_ext = pathinfo($vs_path, PATHINFO_EXTENSION)) { $vs_filename .= ".{$vs_ext}"; } $o_phar->addFile($vs_path, $vs_filename); $vn_c++; } } $o_view = new View($this->request, $this->request->getViewsDirectoryPath() . '/bundles/'); // send download $vs_idno = $t_lot->get('idno_stub'); $o_view->setVar('tmp_file', $vs_tmp_name); $o_view->setVar('download_name', 'media_for_' . mb_substr(preg_replace('![^A-Za-z0-9]+!u', '_', $vs_idno ? $vs_idno : $t_lot->getPrimaryKey()), 0, 20) . '.zip'); $this->response->addContent($o_view->render('ca_object_lots_download_media.php')); return; } else { $this->notification->addNotification(_t('No media is available for download'), __NOTIFICATION_TYPE_ERROR__); $this->opo_response->setRedirect(caEditorUrl($this->opo_request, 'ca_object_lots', $t_lot->getPrimaryKey())); return; } } //} return $this->Edit(); }