public function uploadAction() { if (!is_object($item = $this->_getItem())) { die("Failure finding or creating item"); } // #TODO check CSRF /* $tempDir = sys_get_temp_dir()."/omeka-audio-recorder"; if(!is_dir($tempDir)) $res = mkdir($tempDir,0777); echo ("Res: $res"); */ $filename = 'audio_recording_' . date('Y-m-d-H-i-s') . '.mp3'; $filename = is_null($_POST['fname']) ? $_POST['fname'] : $filename; $filepath = tempnam(sys_get_temp_dir(), 'audio-recording_') . '.mp3'; $data = base64_decode(substr($_POST['data'], strpos($_POST['data'], ",") + 1)); $fp = fopen($filepath, 'wb'); fwrite($fp, $data); fclose($fp); $files = insert_files_for_item($item, 'Filesystem', array('source' => $filepath, 'name' => $filename), array()); $minOrder = 99; foreach ($files as $file) { $file->order = $minOrder; $file->save(); $minOrder++; } die("Success: {$filename}"); }
public function testCanInsertFilesForAnItem() { $fileUrl = TEST_DIR . '/_files/test.txt'; $files = insert_files_for_item($this->item, 'Filesystem', array($fileUrl)); $this->assertEquals(1, count($files)); $this->assertThat($files[0], $this->isInstanceOf('File')); $this->assertTrue($files[0]->exists()); }
/** * Check insertion of a second file with a duplicate name. * * @internal Omeka allows to have two files with the same name. */ protected function _testInsertDuplicateFile() { $fileUrl = $this->_fileUrl; $files = insert_files_for_item($this->item, 'Filesystem', array($fileUrl)); // Retrieve files from the database to get a fully inserted file, with // all updated metadata. $files = $this->item->getFiles(); $this->assertEquals(2, count($files)); // Get the second file. $file = $files[1]; // Generic checks. $this->assertThat($file, $this->isInstanceOf('File')); $this->assertTrue($file->exists()); $this->assertEquals(filesize($fileUrl), $file->size); $this->assertEquals(md5_file($fileUrl), $file->authentication); $this->assertEquals(pathinfo($fileUrl, PATHINFO_BASENAME), $file->original_filename); // Readable filename check. $storageFilepath = $this->item->id . DIRECTORY_SEPARATOR . pathinfo($fileUrl, PATHINFO_FILENAME) . '.1.' . pathinfo($fileUrl, PATHINFO_EXTENSION); $this->assertEquals($storageFilepath, $file->filename); // Readable filepath check. $this->_checkFile($file); }
public function hookAfterSaveItem($args) { $item = $args['record']; $post = $args['post']; if (!($post && isset($post['dropbox-files']))) { return; } $fileNames = $post['dropbox-files']; if ($fileNames) { if (!dropbox_can_access_files_dir()) { throw new Dropbox_Exception(__('The Dropbox files directory must be both readable and writable.')); } $filePaths = array(); foreach ($fileNames as $fileName) { $filePaths[] = dropbox_validate_file($fileName); } $files = array(); try { $files = insert_files_for_item($item, 'Filesystem', $filePaths, array('file_ingest_options' => array('ignore_invalid_files' => false))); } catch (Omeka_File_Ingest_InvalidException $e) { release_object($files); $item->addError('Dropbox', $e->getMessage()); return; } catch (Exception $e) { release_object($files); throw $e; } release_object($files); // delete the files foreach ($filePaths as $filePath) { try { unlink($filePath); } catch (Exception $e) { throw $e; } } } }
/** * Adds a new item based on a row string in the CSV file and returns it. * * @param string $row A row string in the CSV file * @return Item|boolean The inserted item or false if an item could not be added. */ protected function _addItemFromRow($row) { $result = $this->getColumnMaps()->map($row); $tags = $result[CsvImport_ColumnMap::TYPE_TAG]; $itemMetadata = array(Builder_Item::IS_PUBLIC => $this->is_public, Builder_Item::IS_FEATURED => $this->is_featured, Builder_Item::ITEM_TYPE_ID => $this->item_type_id, Builder_Item::COLLECTION_ID => $this->collection_id, Builder_Item::TAGS => $tags); // If this is coming from CSV Report, bring in the itemmetadata coming from the report if (!is_null($result[CsvImport_ColumnMap::TYPE_COLLECTION])) { $itemMetadata[Builder_Item::COLLECTION_ID] = $result[CsvImport_ColumnMap::TYPE_COLLECTION]; } if (!is_null($result[CsvImport_ColumnMap::TYPE_PUBLIC])) { $itemMetadata[Builder_Item::IS_PUBLIC] = $result[CsvImport_ColumnMap::TYPE_PUBLIC]; } if (!is_null($result[CsvImport_ColumnMap::TYPE_FEATURED])) { $itemMetadata[Builder_Item::IS_FEATURED] = $result[CsvImport_ColumnMap::TYPE_FEATURED]; } if (!empty($result[CsvImport_ColumnMap::TYPE_ITEM_TYPE])) { $itemMetadata[Builder_Item::ITEM_TYPE_NAME] = $result[CsvImport_ColumnMap::TYPE_ITEM_TYPE]; } $elementTexts = $result[CsvImport_ColumnMap::TYPE_ELEMENT]; try { $item = insert_item($itemMetadata, $elementTexts); } catch (Omeka_Validator_Exception $e) { $this->_log($e, Zend_Log::ERR); return false; } catch (Omeka_Record_Builder_Exception $e) { $this->_log($e, Zend_Log::ERR); return false; } $fileUrls = $result[CsvImport_ColumnMap::TYPE_FILE]; foreach ($fileUrls as $url) { try { $file = insert_files_for_item($item, 'Url', $url, array('ignore_invalid_files' => false)); } catch (Omeka_File_Ingest_InvalidException $e) { $msg = "Invalid file URL '{$url}': " . $e->getMessage(); $this->_log($msg, Zend_Log::ERR); $item->delete(); release_object($item); return false; } catch (Omeka_File_Ingest_Exception $e) { $msg = "Could not import file '{$url}': " . $e->getMessage(); $this->_log($msg, Zend_Log::ERR); $item->delete(); release_object($item); return false; } release_object($file); } // Makes it easy to unimport the item later. $this->_recordImportedItemId($item->id); return $item; }
/** * Iterate through the $_FILES array for files that have been uploaded * to Omeka and attach each of those files to this Item. */ private function _uploadFiles() { fire_plugin_hook('before_upload_files', array('item' => $this)); // Tell it to always try the upload, but ignore any errors if any of // the files were not actually uploaded (left form fields empty). if (!empty($_FILES['file'])) { $files = insert_files_for_item($this, 'Upload', 'file', array('ignoreNoFile' => true)); } }
/** * Convenience method for inserting an item and its files. * * Method used by map writers that encapsulates item and file insertion. * Items are inserted first, then files are inserted individually. This is * done so Item and File objects can be released from memory, avoiding * memory allocation issues. * * @see insert_item() * @see insert_files_for_item() * @param mixed $metadata Item metadata * @param mixed $elementTexts The item's element texts * @param mixed $fileMetadata The item's file metadata * @return true */ protected final function _insertItem($metadata = array(), $elementTexts = array(), $fileMetadata = array()) { // Insert the item. $item = insert_item($metadata, $elementTexts); // Retrieve the inserted ID $insertedId = $item->id; // Insert the record after the item is saved. The idea here is that the // OaipmhHarvester_Records table should only contain records that have // corresponding items. $this->_insertRecord($item); // If there are files, insert one file at a time so the file objects can // be released individually. if (isset($fileMetadata['files'])) { // The default file transfer type is URL. $fileTransferType = isset($fileMetadata['file_transfer_type']) ? $fileMetadata['file_transfer_type'] : 'Url'; // The default option is ignore invalid files. $fileOptions = isset($fileMetadata['file_ingest_options']) ? $fileMetadata['file_ingest_options'] : array('ignore_invalid_files' => true); // Prepare the files value for one-file-at-a-time iteration. $files = array($fileMetadata['files']); foreach ($files as $file) { $fileOb = insert_files_for_item($item, $fileTransferType, $file, $fileOptions); $fileObject = $fileOb; if (!empty($file['metadata'])) { $fileObject->addElementTextsByArray($file['metadata']); $fileObject->save(); } /* CG35 : deleting temporary files */ foreach ($fileMetadata['files'] as $files) { $cmd = "rm " . $files; shell_exec($cmd); } // Release the File object from memory. release_object($fileObject); } } // Release the Item object from memory. release_object($item); return $insertedId; }
/** * Set some records with identifier to test. */ protected function _prepareRecords() { // Remove default records. $this->_deleteAllRecords(); $metadata = array('public' => true); $isHtml = false; $collections = array(); $items = array(); $files = array(); foreach ($this->_recordsByType as $type => $recordsMetadata) { foreach ($recordsMetadata as $recordMetadata) { $identifiers = array(); foreach ($recordMetadata['Identifier'] as $identifier) { $identifiers[] = array('text' => $identifier, 'html' => $isHtml); } $elementTexts = array('Dublin Core' => array('Title' => array(array('text' => $recordMetadata['Title'], 'html' => $isHtml)), 'Identifier' => $identifiers)); switch ($type) { case 'Collection': $collections[$recordMetadata['collection_id']] = insert_collection($metadata, $elementTexts); break; case 'Item': $metadataItem = $metadata; if (!empty($recordMetadata['collection_id'])) { $metadataItem['collection_id'] = $collections[$recordMetadata['collection_id']]->id; } $record = insert_item($metadataItem, $elementTexts); if (!empty($recordMetadata['files'])) { $fileUrl = TEST_DIR . '/_files/test.txt'; $files[$recordMetadata['item_id']] = insert_files_for_item($record, 'Filesystem', array_fill(0, $recordMetadata['files'], $fileUrl)); } break; case 'File': $record = $files[$recordMetadata['item_id']][$recordMetadata['file_key']]; $record->addElementTextsByArray($elementTexts); $record->save(); break; } } } }
/** * Attach a list of files to an item. * * @param Item $item * @param array $fileUrls An array of the urls of files to attach to item. * @param boolean $itemDelete Delete item (default) or not if the file can't * be ingested. * @return boolean True if success, false else. */ protected function _attachFilesToItem($item, $fileUrls, $itemDelete = true) { // Sometime, fileUrls is a null or an empty string. if (empty($fileUrls)) { return true; } foreach ($fileUrls as $fileUrl) { // Set the transfer strategy according to file name. $parsedFileUrl = parse_url($fileUrl); if (!isset($parsedFileUrl['scheme']) || $parsedFileUrl['scheme'] == 'file') { $transferStrategy = 'Filesystem'; $fileUrlOriginal = $fileUrl; $fileUrl = $parsedFileUrl['path']; if (!$this->_allowLocalPath($fileUrl)) { $msg = 'Local paths are not allowed by the administrator (%s) [%s].'; $this->_log($msg, array($fileUrlOriginal, !isset($parsedFileUrl['scheme']) ? 'no scheme' : 'file scheme'), Zend_Log::ERR); if ($itemDelete) { $item->delete(); } release_object($item); return false; } } else { $transferStrategy = 'Url'; $fileUrl = $this->_rawUrlEncode($fileUrl); } // Import the file and attach it to the item. try { $files = insert_files_for_item($item, $transferStrategy, $fileUrl, array('ignore_invalid_files' => false)); } catch (Omeka_File_Ingest_InvalidException $e) { $msg = 'Invalid file URL "%s": %s'; $this->_log($msg, array($fileUrl, $e->getMessage()), Zend_Log::ERR); if ($itemDelete) { $item->delete(); } release_object($item); return false; } catch (Omeka_File_Ingest_Exception $e) { $msg = 'Could not import file "%s": %s'; $this->_log($msg, array($fileUrl, $e->getMessage()), Zend_Log::ERR); if ($itemDelete) { $item->delete(); } release_object($item); return false; } release_object($files); } return true; }
private function _doFileImportForItem($sp_item, $o_item) { $enclosures = $sp_item->get_enclosures(); $filesArray = array(); foreach ($enclosures as $enclosure) { $enclosureInfoArray = array(); $enclosureInfoArray['source'] = $enclosure->get_link(); $enclousreInfoArray['name'] = $enclosure->get_title(); $enclosureInfoArray['metadata'] = $this->_buildMediaFileMetaData($enclosure); $filesArray[] = $enclosureInfoArray; } try { insert_files_for_item($o_item, 'UrlClio', $filesArray); } catch (Exception $e) { echo "insert fail"; } }
/** * Import a single photo in real time (not in the background). * * This function relies on the import form output being in the * $_POST variable. The form should be validated before calling this. * * @return bool $success true if no error, false otherwise */ private static function _importSingle() { //include the import job class, whose static methods will import the photo require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'jobs' . DIRECTORY_SEPARATOR . 'import.php'; //include the phpFlickr library for interfacing with the Flickr API require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'phpFlickr' . DIRECTORY_SEPARATOR . 'phpFlickr.php'; //initialize a Flickr API interface with this plugin's API key $f = new phpFlickr(get_option('flickr_api_key')); //pull the Flickr url from the form post if (isset($_REQUEST['flickrurl'])) { $url = $_REQUEST['flickrurl']; } else { throw new UnexpectedValueException('URL of Flickr photo was not set'); } $photoID = self::_parsePhotoUrl($url); if (isset($_REQUEST['flickrcollection'])) { $collection = $_REQUEST['flickrcollection']; } else { $collection = 0; } if (isset($_REQUEST['flickrpublic'])) { $public = $_REQUEST['flickrpublic']; } else { $public = false; } if (isset($_REQUEST['flickruserrole'])) { $userRole = $_REQUEST['flickruserrole']; } else { $userRole = 0; } try { //retrive the photo information in the correct format to create a new Omeka item $post = FlickrImport_ImportJob::GetPhotoPost($photoID, $f, $collection, $userRole, $public); //retrieve the files associated with this photo (the photo itself, mainly) //in the correct format to attach to an omeka item $files = FlickrImport_ImportJob::GetPhotoFiles($photoID, $f); } catch (Exception $e) { throw $e; } if ($post == "video") { return false; } //create the item $record = new Item(); $record->setPostData($post); if (!$record->save(false)) { throw new Exception($record->getErrors()); } if (!insert_files_for_item($record, 'Url', $files)) { throw new Exception("Error attaching files"); } return true; }
/** * Attach a new file and its metadata to an item. * * @param array $document A normalized document. * @return File|null The inserted file or null. */ protected function _createFile($document) { // Check if the file url is present. if (empty($document['process']['fullpath'])) { return; } // Get record from the main record, that is saved before. // TODO Get the item via the specific "item" name. $item = $this->_folder->getRecord($this->_indexFirstLevelRecord); if (empty($item)) { $message = __('The file "%s" cannot be created before the item.', $document['specific']['path']); throw new ArchiveFolder_ImporterException($message); } $fileUrl = $document['process']['fullpath']; // Set the transfer strategy according to the file url. $parsedFileUrl = parse_url($fileUrl); if (!isset($parsedFileUrl['scheme']) || $parsedFileUrl['scheme'] == 'file') { $transferStrategy = 'Filesystem'; $fileUrlOriginal = $fileUrl; $fileUrl = $parsedFileUrl['path']; if (!$this->_allowLocalPath($fileUrl)) { $message = __('Local paths are not allowed by the administrator (%s) [%s].', $fileUrlOriginal, !isset($parsedFileUrl['scheme']) ? 'no scheme' : 'file scheme'); throw new ArchiveFolder_ImporterException($message); } } else { $transferStrategy = 'Url'; $fileUrl = $this->_rawUrlEncode($fileUrl); } // Import the file and attach it to the item. try { $files = insert_files_for_item($item, $transferStrategy, $fileUrl, array('ignore_invalid_files' => false)); } catch (Omeka_File_Ingest_InvalidException $e) { $message = __('Error occurred when attempting to ingest "%s" as a file: %s', $fileUrl, $e->getMessage()); throw new ArchiveFolder_ImporterException($message); } // Need to release file in order to update all current data, because // $file->save() is not enough. $fileId = $files[0]->id; release_object($files); $record = get_db()->getTable('File')->find($fileId); // Update the file with metadata. $document['process']['action'] = ArchiveFolder_Importer::ACTION_UPDATE; $record = $this->_updateRecord($record, $document); return $record; }
/** * Import a single video in real time (not in the background). * * This function relies on the import form output being in the * $_POST variable. The form should be validated before calling this. * * @return bool $success true if no error, false otherwise */ private static function _importSingle() { require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'import.php'; require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'Google' . DIRECTORY_SEPARATOR . 'Client.php'; require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'Google' . DIRECTORY_SEPARATOR . 'Service' . DIRECTORY_SEPARATOR . 'YouTube.php'; $client = new Google_Client(); $client->setApplicationName("Omeka_Youtube_Import"); $client->setDeveloperKey(YoutubeImport_ImportHelper::$youtube_api_key); try { $service = new Google_Service_YouTube($client); } catch (Exception $e) { throw $e; } if (isset($_REQUEST['youtubeurl'])) { $url = $_REQUEST['youtubeurl']; } else { throw new UnexpectedValueException('URL of Youtube video was not set'); } if (isset($_REQUEST['youtubecollection'])) { $collection = $_REQUEST['youtubecollection']; } else { $collection = 0; } if (isset($_REQUEST['youtubeuserrole'])) { $ownerRole = $_REQUEST['youtubeuserrole']; } else { $ownerRole = 0; } if (isset($_REQUEST['youtubepublic'])) { $public = $_REQUEST['youtubepublic']; } else { $public = false; } try { $videoID = YoutubeImport_ImportHelper::ParseURL($url); $response = YoutubeImport_ImportHelper::GetVideo($videoID, $service, $collection, $ownerRole, $public); $post = $response['post']; $files = $response['files']; } catch (Exception $e) { throw $e; } $record = new Item(); $record->setPostData($post); if (!$record->save(false)) { throw new Exception($record->getErrors()); } if (!empty($files) && !empty($record)) { if (!insert_files_for_item($record, 'Url', $files)) { throw new Exception("Error attaching files"); } } return true; }
/** * Import a youtube video as submitted by a user through the contribution form * * This function relies on the import form output being in the * $_POST variable. The form should be validated before calling this. * * @return bool $success true if no error, false otherwise */ private static function _importContributedSingle($item) { require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'import.php'; require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'Google' . DIRECTORY_SEPARATOR . 'Client.php'; require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'Google' . DIRECTORY_SEPARATOR . 'Service' . DIRECTORY_SEPARATOR . 'YouTube.php'; $client = new Google_Client(); $client->setApplicationName("Omeka_Youtube_Import"); $client->setDeveloperKey(YoutubeImport_ImportHelper::$youtube_api_key); try { $service = new Google_Service_YouTube($client); } catch (Exception $e) { throw $e; } if (isset($_REQUEST['youtubeurl'])) { $url = $_REQUEST['youtubeurl']; } else { throw new UnexpectedValueException('URL of Youtube video was not set'); } if (!isset($item)) { $item = Item(); } try { $videoID = YoutubeImport_ImportHelper::ParseURL($url); $response = YoutubeImport_ImportHelper::GetVideo($videoID, $service, $item->collection_id, 'Contributor', get_option('contributedItemPublic') && $item->public == 1); $post = $response['post']; $files = $response['files']; } catch (Exception $e) { $item->delete(); throw $e; } $post['collection_id'] = $item['collection_id']; $item->setPostData($post); if (!$item->save(false)) { throw new Exception($item->getErrors()); } if (!empty($files) && !empty($item)) { if (!insert_files_for_item($item, 'Url', $files)) { throw new Exception("Error attaching files"); } } return true; }
/** * Create a new Omeka item with information from a Flickr image * *@param string $itemID The Flickr photo ID of the photo to be added * @return void */ private function _addPhoto($itemID) { try { $post = self::GetPhotoPost($itemID, $this->f, $this->collection, $this->ownerRole, $this->public); $files = self::GetPhotoFiles($itemID, $this->f); } catch (Exception $e) { throw $e; } if (!is_array($post) && $post == 'video') { return 'video'; } $record = new Item(); $record->setPostData($post); if (!$record->save(false)) { throw new Exception("Error saving new omeka item to database"); } try { insert_files_for_item($record, 'Url', $files); } catch (Exception $e) { throw $e; } }