/** * Overload save() to process a photo stored in $file * * @return int */ public function save($validation = NULL) { // Process the photo if there is one if ($this->file) { $path_info = pathinfo($this->file); $ext = $path_info['extension']; // Make a unique name for this $this->filename = uniqid(NULL, TRUE) . '.' . $ext; // Shard the file on the filesystem $shards = array(); for ($i = 0; $i <= 4; $i += 2) { $shards[] = substr($this->filename, $i, 2); } $path = APPPATH . 'photos/' . implode('/', $shards) . '/'; if (!is_dir($path)) { mkdir($path, 0755, TRUE); } if (is_uploaded_file($this->file)) { move_uploaded_file($this->file, $path . $this->filename); } else { copy($this->file, $path . $this->filename); } } try { return parent::save($validation); } catch (AutoModeler_Exception $e) { // Delete the photo we uploaded, since something went wrong if ($this->file) { unlink($path . $this->filename); // TODO: delete the straggler directory } throw $e; } }
/** * Creates or updates the current exif data. * * @return boolean */ public function save($validation = null) { // If new EXIF data, try to read from image if (!$this->loaded()) { $this->read(); } // If was new and no exif data was found it will not be saved return parent::save(); }
/** * Override save() to set date_created if this is a new object * * @param object $validation a validation object to save with * * @return int */ public function save($validation = NULL) { if ($this->id) { throw new Kohana_Exception('Saved orders cannot be modified!'); } if (!$this->id) { $this->date_created = time(); } // Save the related items if this is a new item if (!$this->id and $status = parent::save($validation)) { foreach ($this->_order_products as $product) { $order_product = new Model_Order_Product(); $order_product->order_id = $this->id; $order_product->product_id = $product['product']->id; $order_product->quantity = $product['quantity']; $order_product->save(); } } return $status; }
/** * Creates or updates the current image. * * @param Validation $validation a manual validation object to combine the model properties with * @return integer * * @throws Kohana_Exception */ public function save(Validation $validation = null) { $new = !(bool) $this->id; // Validate new image if ($new) { $path = Kohana::$config->load('image.upload_path'); // Download remote files if ($this->remote && !$this->file) { $this->file = Request::factory($this->remote)->download(null, $path); } if (!$this->file || !$this->remote && !Upload::not_empty($this->file)) { throw new Kohana_Exception(__('No image')); } else { if (!Upload::size($this->file, Kohana::$config->load('image.filesize'))) { throw new Kohana_Exception(__('Image too big (limit :size)', array(':size' => Kohana::$config->load('image.filesize')))); } else { if (!Upload::type($this->file, Kohana::$config->load('image.filetypes')) && !in_array($this->file['type'], Kohana::$config->load('image.mimetypes'))) { throw new Kohana_Exception(__('Invalid image type (use :types)', array(':types' => implode(', ', Kohana::$config->load('image.filetypes'))))); } } } $upload = $this->file; if ($this->remote && !is_uploaded_file($upload['tmp_name'])) { // As a remote file is no actual file field, manually set the filename $this->file = basename($upload['tmp_name']); } else { if (is_uploaded_file($upload['tmp_name'])) { // Sanitize the filename $upload['name'] = preg_replace('/[^a-z0-9-\\.]/', '-', mb_strtolower($upload['name'])); // Strip multiple dashes $upload['name'] = preg_replace('/-{2,}/', '-', $upload['name']); // Try to save upload if (false !== ($this->file = Upload::save($upload, null, $path))) { // Get new filename $this->file = basename($this->file); } } } } try { parent::save(); } catch (Validation_Exception $e) { if ($new && $this->file) { unlink($path . $this->file); } throw $e; } // Some magic on created images only if ($new) { // Make sure we have the new target directory $new_path = Kohana::$config->load('image.path') . URL::id($this->id); if (!is_dir($new_path)) { mkdir($new_path, 0777, true); chmod($new_path, 0777); } if (is_writable($new_path)) { $new_path = rtrim($new_path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; } else { throw new Kohana_Exception(get_class($this) . ' can not write to directory'); } // New file name with some random postfix for hard to guess filenames !$this->postfix and $this->postfix = Text::random('alnum', 8); $new_file = $this->id . '_' . $this->postfix . Kohana::$config->load('image.postfix_original') . '.jpg'; // Rename and move to correct directory using image id $old_file = $this->file; if (!rename($path . $old_file, $new_path . $new_file)) { unlink($path . $old_file); throw new Kohana_Exception(get_class($this) . ' could not move uploaded image'); } $this->file = $new_file; // Start creating images $this->_generate_images($new_path . $new_file); parent::save(); } return $this; }
/** * Save model and expire caches. * * @param mixed $validation * @return integer */ public function save($validation = null) { if ($result = parent::save($validation)) { self::expire_caches($this->id); } return $result; }