/** * doAjax * * @return mixed */ protected function doExecute() { if (!$this->app->get('unidev.image.storage')) { throw new \LogicException('No image storage set in config.'); } $file = $this->input->files->get($this->fieldName); $folder = $this->input->getPath('folder'); $folder = ltrim($folder . '/', '/'); if ($file->getError()) { throw new \RuntimeException('Upload fail: ' . UploadedFileHelper::getUploadMessage($file->getError()), 500); } $id = $this->getImageName($file->getClientFilename()); $temp = $this->getImageTemp($id, File::getExtension($file->getClientFilename())); if (!is_dir(dirname($temp))) { Folder::create(dirname($temp)); } $file->moveTo($temp); $temp = $this->resize($temp); if (!is_file($temp)) { throw new \RuntimeException('Temp file not exists'); } $url = ImageUploader::upload($temp, $this->getImagePath($folder . $id, File::getExtension($temp))); File::delete($temp); $this->addMessage('Upload success.'); return array('url' => $url); }
/** * Do this execute. * * @return mixed */ protected function doExecute() { $dest = $this->config['dir.dest']; $migName = $this->config['replace.controller.item.name.cap'] . 'Init'; if (!is_dir($dest . '/Migration')) { return; } // Copy migration $files = Folder::files($dest . '/Migration'); $file = false; // If last migration with same name exists, delete duplicated migration. foreach ($files as $file) { $fileName = pathinfo($file, PATHINFO_BASENAME); if (strpos($file, $migName . '.php') !== false && $fileName != '19700101000000_' . $migName . '.php') { if (is_file($dest . '/Migration/' . '19700101000000_' . $migName . '.php')) { File::delete($dest . '/Migration/' . '19700101000000_' . $migName . '.php'); } return; } } foreach ($files as $file) { if (strpos($file, $migName . '.php') !== false) { break; } } // Migration not exists, return. if (!$file) { return; } $newName = gmdate('YmdHis') . '_' . $migName . '.php'; File::move($file, $dest . '/Migration/' . $newName); $this->io->out('[<info>Action</info>] Rename migration file to: ' . $newName); }
/** * delete * * @param string $path * * @return bool */ public static function delete($path) { if (is_dir($path)) { Folder::delete($path); } elseif (is_file($path)) { File::delete($path); } return true; }
/** * Execute the controller. * * @return mixed Return executed result. * * @throws \LogicException * @throws \RuntimeException */ public function execute() { $files = $this->input->files; $field = $this->input->get('field', 'file'); $id = $this->input->get('id'); $author = Author::getAuthor($id); $user = User::get(); $blog = Blog::get(); try { if (!Author::isAdmin($blog, $user)) { throw new ValidFailException('You cannot edit this author.'); } $src = $files->getByPath($field . '.tmp_name', null, InputFilter::STRING); $name = $files->getByPath($field . '.name', null, InputFilter::STRING); if (!$src) { throw new \Exception('File not upload'); } $ext = pathinfo($name, PATHINFO_EXTENSION); $uuid = $author->uuid ?: Uuid::v4(); $src = Thumb::createThumb($src); $dest = sprintf('author/%s/%s.%s', sha1($uuid), md5($uuid), $ext); $result = S3Helper::put($src, $dest); File::delete($src); if (!$result) { throw new \Exception('Upload fail.'); } } catch (\Exception $e) { $response = new Response(); $response->setBody(json_encode(['error' => $e->getMessage()])); $response->setMimeType('text/json'); $response->respond(); exit; } $return = new Registry(); $return['filename'] = 'https://windspeaker.s3.amazonaws.com/' . $dest; $return['file'] = 'https://windspeaker.s3.amazonaws.com/' . $dest; $return['uuid'] = $uuid; if ($author->id) { $author->image = $return['filename']; (new DataMapper('authors'))->updateOne($author); } $response = new Response(); $response->setBody((string) $return); $response->setMimeType('text/json'); $response->respond(); exit; }
/** * Execute the controller. * * @return mixed Return executed result. * * @throws \LogicException * @throws \RuntimeException */ public function execute() { $files = $this->input->files; $field = $this->input->get('field', 'file'); $user = User::get(); try { $src = $files->getByPath($field . '.tmp_name', null, InputFilter::STRING); $name = $files->getByPath($field . '.name', null, InputFilter::STRING); if (!$src) { throw new \Exception('File not upload'); } $ext = pathinfo($name, PATHINFO_EXTENSION); $src = Thumb::createThumb($src); $dest = sprintf('user/%s/%s.%s', sha1('user-profile-' . $user->id), md5('user-profile-' . $user->id), $ext); $result = S3Helper::put($src, $dest); File::delete($src); if (!$result) { throw new \Exception('Upload fail.'); } } catch (\Exception $e) { $response = new Response(); $response->setBody(json_encode(['error' => $e->getMessage()])); $response->setMimeType('text/json'); $response->respond(); exit; } $return = new Registry(); $return['filename'] = 'https://windspeaker.s3.amazonaws.com/' . $dest; $return['file'] = 'https://windspeaker.s3.amazonaws.com/' . $dest; $user->image = $return['filename']; User::save($user); $response = new Response(); $response->setBody((string) $return); $response->setMimeType('text/json'); $response->respond(); exit; }
/** * Delete a folder. * * @param string $path The path to the folder to delete. * * @return boolean True on success. * * @since 2.0 * @throws FilesystemException * @throws \UnexpectedValueException */ public static function delete($path) { @set_time_limit(ini_get('max_execution_time')); // Sanity check if (!rtrim($path, '/\\')) { // Bad programmer! Bad Bad programmer! throw new FilesystemException(__METHOD__ . ': You can not delete a base directory.'); } // Check to make sure the path valid and clean $path = Path::clean($path); // Is this really a folder? if (!is_dir($path)) { throw new \UnexpectedValueException(sprintf('%1$s: Path is not a folder. Path: %2$s', __METHOD__, $path)); } // Remove all the files in folder if they exist; disable all filtering $files = static::files($path); if (!empty($files)) { File::delete($files); } // Remove sub-folders of folder; disable all filtering $folders = static::folders($path); foreach ($folders as $folder) { if (is_link($folder)) { // Don't descend into linked directories, just delete the link. File::delete($folder); } else { static::delete($folder); } } // In case of restricted permissions we zap it one way or the other // as long as the owner is either the webserver or the ftp. if (@rmdir($path)) { return true; } else { throw new FilesystemException(sprintf('%1$s: Could not delete folder. Path: %2$s', __METHOD__, $path)); } }
/** * Method to test delete(). * * @return void * * @covers Windwalker\Filesystem\File::delete */ public function testDelete() { File::delete(static::$dest . '/folder1/path1'); $this->assertFileNotExists(static::$dest . '/folder1/path1'); try { File::delete(static::$dest . '/folder1/path2'); } catch (FilesystemException $e) { $this->assertInstanceOf('Windwalker\\Filesystem\\Exception\\FilesystemException', $e); } }
/** * copy * * @param string $src * @param string $dest * * @return void */ protected function copyFile($src, $dest) { $this->out('<info>Create</info>: ' . $dest); if (is_file($dest)) { File::delete($dest); } File::copy($src, $dest); }
/** * quickUpload * * @param string $base64 * @param string $uri * * @return string */ public static function quickUpload($base64, $uri) { $ext = Base64Image::getTypeFromBase64($base64); if (!$ext) { return false; } $temp = WINDWALKER_TEMP . '/unidev/images/temp/' . gmdate('Ymd') . '/' . md5(uniqid(mt_rand(1, 999))) . '.' . $ext; if (!is_dir(dirname($temp))) { Folder::create(dirname($temp)); } Base64Image::toFile($base64, $temp); // Upload to Cloud $url = ImageUploader::upload($temp, $uri); if (is_file($temp)) { File::delete($temp); } return $url; }