public static function remove($key, $asMask = false) { $key = Str::toURIParam($key); if (!$asMask) { $path = static::$path . $key . ".cache"; return File::delete($path); } else { $deleted = false; $files = File::lsdir(static::$path, ".cache"); foreach ($files as $file) { if (Str::contains($file, $key)) { File::delete(static::$path . $file); $delete = true; } } return $deleted; } }
public function save($value) { $flag = $this->module->flag; $path = File::formatDir($this->path); $destPath = static::storagePath() . File::formatDir($this->path); File::mkdir($destPath); if (!is_writable($destPath)) { Response::code(500); return Response::json(array("error" => true, "error_description" => "Directory '" . $destPath . "' is not writtable.")); } $files = json_decode(Session::get($this->sessionKey), true); if ($flag == "U" || $flag == "D") { $oldFiles = $this->module->orm->field($this->name); if (!is_array(@json_decode($oldFiles, true))) { $oldFiles = json_encode(array()); } $oldFiles = json_decode($oldFiles, true); if ($flag == "U") { //Delete files that are on our table but not on our session list foreach ($oldFiles as $oldFile) { $found = false; foreach ($files as $file) { if (!array_key_exists("_tmpName", $file) && current($oldFile) == current($file)) { $found = true; break; } } if (!$found) { foreach ($oldFile as $sample) { File::delete(static::storagePath() . $sample); } } } } else { if ($flag == "D") { //Delete all files foreach ($oldFiles as $oldFile) { foreach ($oldFile as $sample) { File::delete(static::storagePath() . $sample); } } if (count(File::lsdir($destPath)) == 0) { File::rmdir($destPath); } } } } if ($flag == "C" || $flag == "U") { //Copy tmp files to it's target place and save foreach ($files as $k => $file) { if (array_key_exists("_tmpName", $file)) { $samples = array(); $im = new Image(); $i = 0; $tmpPath = static::tmpPath() . $file["_tmpName"]; foreach ($this->samples as $k2 => $sample) { //if ($k2 == 0) //{ $im->load($tmpPath); //} if (!array_key_exists("width", $sample)) { $sample["width"] = 0; } if (!array_key_exists("height", $sample)) { $sample["height"] = 0; } if ($sample["width"] != 0 || $sample["height"] != 0) { $im->resize($sample["width"], $sample["height"], $sample["resizeMethod"], $sample["background"]); } if ($this->customFilters) { foreach ($this->customFilters as $filter) { call_user_func($filter, $im); } } $fileName = $file["_name"]; if ($sample["key"] != "original") { $fileName = File::removeExtension($file["_name"]) . "-" . $sample["key"] . "." . File::extension($file["_name"]); } $unique = static::unique($destPath . $fileName); $im->save($unique); $samples[$sample["key"]] = $path . File::fileName($unique); $samples["caption"] = array_key_exists('caption', $file) ? $file['caption'] : ""; $i++; } File::delete($tmpPath); $files[$k] = $samples; } } $this->module->orm->setField($this->name, json_encode($files)); } }
public function save($value) { $flag = $this->module->flag; $path = File::formatDir($this->path); $destPath = static::storagePath() . File::formatDir($this->path); File::mkdir($destPath); if (!is_writable($destPath)) { Response::code(500); return Response::json(array("error" => true, "error_description" => "Directory '" . $destPath . "' is not writtable.")); } $files = json_decode(Session::get($this->sessionKey), true); if ($flag == "U" || $flag == "D") { $oldFiles = $this->module->orm->field($this->name); if (!is_array(@json_decode($oldFiles, true))) { $oldFiles = json_encode(array()); } $oldFiles = json_decode($oldFiles, true); if ($flag == "U") { //Delete files that are on our table but not on our session list foreach ($oldFiles as $oldFile) { $found = false; foreach ($files as $file) { if (!array_key_exists("_tmpName", $file) && $oldFile["path"] == $file["path"]) { $found = true; break; } } if (!$found) { File::delete(static::storagePath() . $oldFile["path"]); } } } else { if ($flag == "D") { //Delete all files foreach ($oldFiles as $oldFile) { File::delete(static::storagePath() . $oldFile["path"]); } if (count(File::lsdir($destPath)) == 0) { File::rmdir($destPath); } } } } if ($flag == "C" || $flag == "U") { //Copy tmp files to it's target place and save foreach ($files as $k => $file) { if (array_key_exists("_tmpName", $file)) { $unique = static::unique($destPath . $file["_name"]); File::move(static::tmpPath() . $file["_tmpName"], $unique); $files[$k] = array("path" => $path . File::fileName($unique), "caption" => array_key_exists('caption', $file) ? $file['caption'] : ""); } } $this->module->orm->setField($this->name, json_encode($files)); } }