public function generate_image($type, $filter_chain) { try { $original = new midcom_db_attachment($this->attachment); } catch (midcom_error $e) { $e->log(); return false; } $found_derived = false; try { $derived = new midcom_db_attachment($this->{$type}); $found_derived = true; } catch (midcom_error $e) { $derived = new midcom_db_attachment(); $derived->parentguid = $original->parentguid; $derived->title = $original->title; $derived->mimetype = $original->mimetype; $derived->name = $type . '_' . $original->name; } $imagefilter = new midcom_helper_imagefilter($original); if (!$imagefilter->process_chain($filter_chain)) { throw new midcom_error('Image processing failed'); } if (!$found_derived) { if (!$derived->create()) { throw new midcom_error('Failed to create derived image: ' . midcom_connection::get_error_string()); } $this->{$type} = $derived->id; $this->update(); } return $imagefilter->write($derived); }
public function testCRUD() { midcom::get('auth')->request_sudo('midcom.core'); $attachment = new midcom_db_attachment(); $stat = $attachment->create(); $this->assertFalse($stat, midcom_connection::get_error_string()); $attachment = new midcom_db_attachment(); $attachment->parentguid = self::$_topic->guid; $stat = $attachment->create(); $this->assertTrue($stat, midcom_connection::get_error_string()); $this->register_object($attachment); $attachment->refresh(); $this->assertEquals('application/octet-stream', $attachment->mimetype); $this->assertFalse(empty($attachment->location)); $attachment->name = 'test.jpg'; $stat = $attachment->update(); $this->assertTrue($stat); $this->assertEquals('test.jpg', $attachment->name); $stat = $attachment->delete(); $this->assertTrue($stat); midcom::get('auth')->drop_sudo(); }
private function _upload_image(array $file, org_openpsa_slideshow_image_dba $image) { $attachment = new midcom_db_attachment(); $attachment->name = midcom_db_attachment::safe_filename($file['name']); $attachment->title = $_POST['title']; $attachment->mimetype = $file['type']; $attachment->parentguid = $image->guid; if (!$attachment->create() || !$attachment->copy_from_file($file['tmp_name'])) { throw new midcom_error('Failed to create attachment: ' . midcom_connection::get_error_string()); } $this->_response->filename = $attachment->name; $image->attachment = $attachment->id; $image->generate_image('thumbnail', $this->_config->get('thumbnail_filter')); $image->generate_image('image', $this->_config->get('image_filter')); $image->update(); }
/** * Creates a new attachment at the current object and returns it for usage. * * @param midcom_core_dbaobject $object The DBA object we're working on * @param string $name The name of the attachment. * @param string $title The title of the attachment. * @param string $mimetype The MIME-Type of the attachment. * @return midcom_db_attachment The created attachment or false on failure. */ public static function create_attachment(midcom_core_dbaobject $object, $name, $title, $mimetype) { if (!$object->id) { debug_add('Cannot create attachments on a non-persistant object.', MIDCOM_LOG_WARN); return false; } if (!$object->can_do('midgard:update') || !$object->can_do('midgard:attachments')) { debug_add("Failed to set parameters, midgard:update or midgard:attachments on the " . get_class($object) . " {$object->guid} not granted for the current user.", MIDCOM_LOG_ERROR); return false; } $attachment = new midcom_db_attachment(); $attachment->name = $name; $attachment->title = $title; $attachment->mimetype = $mimetype; $attachment->parentguid = $object->guid; $result = $attachment->create(); if (!$result || !$attachment->id) { debug_add("Could not create the attachment '{$name}' for " . get_class($object) . " {$object->guid}: " . midcom_connection::get_error_string(), MIDCOM_LOG_INFO); debug_add('Return code was: ' . $result); return false; } return $attachment; }
private function _process_form() { if (!isset($_POST['midgard_admin_asgard_save'])) { return false; } // Check if we have an uploaded file if (isset($_FILES['midgard_admin_asgard_file']) && is_uploaded_file($_FILES['midgard_admin_asgard_file']['tmp_name'])) { return $this->_process_file_upload($_FILES['midgard_admin_asgard_file']); } if (is_null($this->_file)) { if (empty($_POST['midgard_admin_asgard_filename'])) { return false; } // We're creating a new file $local_filename = midcom_db_attachment::safe_filename($_POST['midgard_admin_asgard_filename']); $local_file = $this->_get_file($local_filename); if (!$local_file) { // New file, create $local_file = new midcom_db_attachment(); $local_file->name = $local_filename; $local_file->parentguid = $this->_object->guid; if (!$local_file->create()) { throw new midcom_error('Failed to create attachment, reason: ' . midcom_connection::get_error_string()); } } } else { $local_file = $this->_file; } $success = true; if (!empty($_POST['midgard_admin_asgard_filename']) && $local_file->name != $_POST['midgard_admin_asgard_filename']) { $local_file->name = $_POST['midgard_admin_asgard_filename']; if (!$local_file->update()) { $success = false; } } if (!empty($_POST['midgard_admin_asgard_mimetype']) && $local_file->mimetype != $_POST['midgard_admin_asgard_mimetype']) { $local_file->mimetype = $_POST['midgard_admin_asgard_mimetype']; if (!$local_file->update()) { $success = false; } } // We should always store at least an empty string so it can be edited later $contents = ''; if (!empty($_POST['midgard_admin_asgard_contents'])) { $contents = $_POST['midgard_admin_asgard_contents']; } if (!$local_file->copy_from_memory($contents)) { $success = false; } if (!$success) { return false; } return $local_file->name; }