function insert($db, $r, $fetch_enclosure = false) { $user = $r->getUser('http'); //allows service user to override created_by_eid $author = $this->getAuthorName(); if ($user->is_serviceuser && $author) { $created_by_eid = $author; } else { $created_by_eid = $user->eid; } $c = Dase_DBO_Collection::get($db, $r->get('collection_ascii_id')); if (!$c) { return; } $sn = Dase_Util::makeSerialNumber($r->slug); $item = $c->createNewItem($sn, $created_by_eid); foreach ($this->getMetadata() as $att => $keyval) { //creates atribute if it doesn't exist! Dase_DBO_Attribute::findOrCreate($db, $c->ascii_id, $att); foreach ($keyval['values'] as $v) { if (trim($v['text'])) { $val = $item->setValue($att, $v['text'], null, $v['mod']); } } } foreach ($this->getMetadataLinks() as $att => $keyval) { Dase_DBO_Attribute::findOrCreate($db, $c->ascii_id, $att); foreach ($keyval['values'] as $v) { if (trim($v['text'])) { //check that it's proper collection if ($c->ascii_id = $v['coll']) { //don't index just yet (the false param) $val = $item->setValueLink($att, $v['text'], $v['url'], $v['mod'], false); } } } } //item_type $item_type = $this->getItemType(); if ($item_type['term']) { $item->setItemType($item_type['term']); } //content if ($this->getContent()) { $item->setContent($this->getContent(), $eid, $this->getContentType()); } //$item->setValue('title',$this->getTitle()); //$item->setValue('description',$this->getSummary()); if ($fetch_enclosure) { $enc = $this->getEnclosure(); if ($enc) { $upload_dir = MEDIA_DIR . '/' . $c->ascii_id . '/uploaded_files'; if (!file_exists($upload_dir)) { $r->renderError(401, 'missing upload directory'); } $ext = Dase_File::$types_map[$enc['type']]['ext']; $new_file = $upload_dir . '/' . $item->serial_number . '.' . $ext; file_put_contents($new_file, file_get_contents($enc['href'])); try { $file = Dase_File::newFile($db, $new_file, $enc['mime_type']); $media_file = $file->addToCollection($item, false, MEDIA_DIR); } catch (Exception $e) { $r->renderError(500, 'could not ingest enclosure file (' . $e->getMessage() . ')'); } } } //messy $item->expireCaches($r->getCache()); $item->buildSearchIndex(); return $item; }
public function postToUploader($r) { //form can use any 'name' it wishes $filecount = 0; foreach ($r->_files as $k => $v) { $input_name = $k; if ($input_name && is_file($r->_files[$input_name]['tmp_name'])) { $name = $r->_files[$input_name]['name']; $path = $r->_files[$input_name]['tmp_name']; $type = $r->_files[$input_name]['type']; if (!Dase_Media::isAcceptable($type)) { Dase_Log::debug(LOG_FILE, $type . ' is not a supported media type'); //$r->renderError(415,'unsupported media type: '.$type); continue; } if (!is_uploaded_file($path)) { //$r->renderError(400,'no go upload'); continue; } Dase_Log::info(LOG_FILE, 'uploading file ' . $name . ' type: ' . $type); try { //this'll create thumbnail, viewitem, and any derivatives $file = Dase_File::newFile($this->db, $path, $type, $name, BASE_PATH); } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'add to collection error: ' . $e->getMessage()); //$r->renderError(409,$e->getMessage()); continue; } $item = $this->collection->createNewItem(null, $this->user->eid); if ($r->has('title')) { $item->setValue('title', $r->get('title')); } else { $item->setValue('title', $name); } try { $media_file = $file->addToCollection($item, true, MEDIA_DIR); //true means tets for dups } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'add to collection error: ' . $e->getMessage()); //$r->renderError(409,$e->getMessage()); continue; } $item->setItemType($r->get('item_type')); //here's where we map admin_att to real att $item->mapConfiguredAdminAtts(); //delay search building?? $item->buildSearchIndex(); $filecount++; } } $r->renderResponse('uploaded ' . $filecount . ' files'); //$r->renderRedirect('manage/'.$this->collection->ascii_id.'/uploader'); }
/** like media->putMedia but this does NOT * delete existing media files */ public function postToMedia($r) { $item = $this->item; $coll = $item->getCollection(); if (!isset($_SERVER['CONTENT_LENGTH']) || !isset($_SERVER['CONTENT_TYPE'])) { $r->renderError(411, 'missing content length'); } $content_type = $r->getContentType(); if ('multipart/form-data' == $content_type) { $user = $r->getUser(); if (!$user->can('write', $this->item)) { $r->renderError(401, 'cannot post media to this item'); } foreach ($r->_files as $k => $v) { $input_name = $k; break; //just get the first one } if ($input_name && is_file($r->_files[$input_name]['tmp_name'])) { $name = $r->_files[$input_name]['name']; $path = $r->_files[$input_name]['tmp_name']; $type = $r->_files[$input_name]['type']; if (!Dase_Media::isAcceptable($type)) { Dase_Log::debug(LOG_FILE, $type . ' is not a supported media type'); $r->renderError(415, 'unsupported media type: ' . $type); } if (!is_uploaded_file($path)) { $r->renderError(400, 'no go upload'); } $bits = file_get_contents($path); $content_type = $type; } } else { $user = $r->getUser('service'); if (!$user->can('write', $this->item)) { $r->renderError(401, 'cannot post media to this item'); } $content_type = Dase_Media::isAcceptable($content_type); if (!$content_type) { $r->renderError(415, 'not an accepted media type'); } $bits = $r->getBody(); } $orig_name = ''; if ($r->http_title) { $item->setValue('title', $r->http_title); $orig_name = $r->http_title; } elseif ($r->slug) { $item->setValue('title', $r->slug); $orig_name = $r->slug; } $upload_dir = MEDIA_DIR . '/' . $coll->ascii_id . '/uploaded_files'; if (!file_exists($upload_dir)) { Dase_Log::debug(LOG_FILE, 'missing upload directory ' . $upload_dir); $r->renderError(500, 'missing upload directory ' . $upload_dir); } $new_file = $upload_dir . '/' . $item->serial_number; $ifp = @fopen($new_file, 'wb'); if (!$ifp) { Dase_Log::debug(LOG_FILE, 'cannot write file ' . $new_file); $r->renderError(500, 'cannot write file ' . $new_file); } @fwrite($ifp, $bits); fclose($ifp); // Set correct file permissions @chmod($new_file, 0644); try { $file = Dase_File::newFile($this->db, $new_file, $content_type, $orig_name, BASE_PATH); //this'll create thumbnail, viewitem, and any derivatives //then return the Dase_DBO_MediaFile for the original $media_file = $file->addToCollection($item, false, MEDIA_DIR); //set 2nd param to true to test for dups } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'item handler error: ' . $e->getMessage()); //delete uploaded file unlink($new_file); $r->renderError(500, 'could not ingest media file (' . $e->getMessage() . ')'); } $item->expireCaches($r->getCache()); $item->buildSearchIndex(); //delete uploaded file unlink($new_file); if ($r->get('page_link')) { $r->renderRedirect($r->get('page_link') . '#' . time()); } //the returned atom entry links to derivs! $mle_url = $r->app_root . '/media/' . $media_file->p_collection_ascii_id . '/' . $media_file->p_serial_number . '.atom'; header("Location:" . $mle_url, TRUE, 201); $r->response_mime_type = 'application/atom+xml'; $r->renderResponse($media_file->asAtom($r->app_root)); }
/** used for swap-out */ public function putMedia($r) { $item = Dase_DBO_Item::get($this->db, $this->collection_ascii_id, $this->serial_number); if (!$item) { $r->renderError(404, 'no such item'); } if (!$this->user->can('write', $item)) { $r->renderError(401, 'cannot put media to this item'); } $coll = $item->getCollection(); if (!isset($_SERVER['CONTENT_LENGTH']) || !isset($_SERVER['CONTENT_TYPE'])) { $r->renderError(411, 'missing content length'); } $content_type = Dase_Media::isAcceptable($r->getContentType()); if (!$content_type) { $r->renderError(415, 'not an accepted media type'); } $bits = $r->getBody(); $upload_dir = MEDIA_DIR . '/' . $coll->ascii_id . '/uploaded_files'; if (!file_exists($upload_dir)) { $r->renderError(500, 'missing upload directory ' . $upload_dir); } $new_file = $upload_dir . '/' . $item->serial_number; $ifp = @fopen($new_file, 'wb'); if (!$ifp) { $r->renderError(500); } @fwrite($ifp, $bits); fclose($ifp); // Set correct file permissions @chmod($new_file, 0644); try { $file = Dase_File::newFile($this->db, $new_file, $content_type, null, BASE_PATH); //since we are swapping in: $item->deleteAdminValues(); //note: this deletes ALL media!!! $item->deleteMedia(MEDIA_DIR); $media_file = $file->addToCollection($item, false, MEDIA_DIR); //set 2nd param to true to test for dups unlink($new_file); } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'media handler error: ' . $e->getMessage()); $r->renderError(500, 'could not ingest file (' . $e->getMessage() . ')'); } $item->buildSearchIndex(); $r->renderOk(); }
private function _newUriMediaResource($r) { $eid = $r->getUser('http')->eid; $url = $r->getBody(); $filename = array_pop(explode('/', $url)); $ext = array_pop(explode('.', $url)); $upload_dir = MEDIA_DIR . '/' . $this->collection->ascii_id . '/uploaded_files'; if (!file_exists($upload_dir)) { $r->renderError(500, 'missing upload directory'); } $item = $this->collection->createNewItem(null, $eid); $item->setValue('title', urldecode($filename)); $new_file = $upload_dir . '/' . $item->serial_number . '.' . $ext; file_put_contents($new_file, file_get_contents($url)); try { $file = Dase_File::newFile($this->db, $new_file, null, null, BASE_PATH); //$media_file = $file->addToCollection($item,true,MEDIA_DIR); //check for dups //accept dups $media_file = $file->addToCollection($item, false, MEDIA_DIR); //check for dups $item->mapConfiguredAdminAtts(); $item->buildSearchIndex(); } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'coll handler error: ' . $e->getMessage()); $item->expunge(); $r->renderError(409, 'could not ingest uri resource (' . $e->getMessage() . ')'); } header("HTTP/1.1 201 Created"); header("Content-Type: text/plain"); header("Location: " . $r->app_root . "/item/" . $r->get('collection_ascii_id') . "/" . $item->serial_number); echo $filename; exit; }