Files
thetool/application/File/File.php

136 lines
3.3 KiB
PHP

<?php
class File extends mfBaseModel {
private $creator;
private $editor;
public static function createFile($filename = false, $overwrite = false) {
// make filename
}
protected function beforeSave() {
if($this->mimetype) {
return true;
}
$this->mimetype = $this->getMimetype();
}
public function fileExists() {
if(!$this->store_filename) return false;
$path = MFUPLOAD_FILE_SAVE_PATH;
$path .= ($this->subfolder) ? "/".$this->subfolder : "";
$path .= "/".$this->store_filename;
return file_exists($path);
}
public function getMimetype() {
if(!$this->store_filename) return false;
$filepath = $this->getFullPath();
if(!file_exists($filepath)) return false;
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($filepath);
return $mime;
}
public function delete() {
if($this->id) {
$id = $this->id;
// delete physical file
if(!unlink($this->getFullPath())) {
$this->log->warn(__CLASS__."::delete(): Error unlinking file ({$this->getFullPath()})");
}
$where = "id=$id";
if($this->db->delete($this->table,$where)) {
if(method_exists($this, "afterDelete")) {
$this->afterDelete();
}
$this->data = new stdClass();
$this->id = "";
return true;
}
}
return false;
}
public function asBase64String() {
$path = $this->getFullPath();
if(!file_exists($path) || (!is_file($path) && !is_link($path))) {
return TT_IMAGE_PLACEHOLDER_B64;
}
return base64_encode(file_get_contents($path));
}
public function asDataUrl() {
$path = $this->getFullPath();
if(!file_exists($path) || (!is_file($path) && !is_link($path))) {
$mime = "image/webp";
$base64 = TT_IMAGE_PLACEHOLDER_B64;
} else {
$base64 = $this->asBase64String();
$mime = $this->getMimetype();
}
return "data:$mime;base64,$base64";
}
public function getFullPath() {
/*if(!is_numeric($this->id) || $this->id < 1) {
throw new Exception("File not found", 4040);
}*/
$filename = $this->store_filename;
$path = MFUPLOAD_FILE_SAVE_PATH;
$path .= ($this->subfolder) ? "/".$this->subfolder : "";
$path .= "/$filename";
if(!file_exists($path)) {
throw new Exception("File not found $path ", 4041);
}
return $path;
}
public function getContents() {
$outname = $this->getFullPath();
return file_get_contents($outname);
}
public function getProperty($name) {
if($this->$name == null) {
if(!$this->id) {
return null;
}
if($name == "creator") {
$this->creator = new User($this->create_by);
return $this->creator;
}
if($name == "editor") {
$this->editor = new User($this->edit_by);
return $this->editor;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
if($this->$name->id) {
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}