mime = ""; $this->version = 0; $this->static = false; $this->basePath = $basePath; $this->fileGroupPermission = new ArrayCollection(); $this->hidden = false; if(is_null($this->basePath)) $this->basePath = __DIR__.'/../../../../app/'; $this->permission = FileWebPermission::PRIVATE; $this->inQueue = false; $this->dateUpd = new \DateTime(); $this->attributes = array(); $this->move = false; $this->encrypted = false; } /** * Sets file. * * @param UploadedFile $file * @return $this */ public function setFile(UploadedFile $file = null) { $this->file = $file; $this->size = $file->getSize(); // check if we have an old image path if (isset($this->path)) { // store the old name to delete after the update $this->path = null; } else { $this->path = 'initial'; } return $this; } /** * Get file. * * @return UploadedFile */ public function getFile() { return $this->file; } public function getAbsolutePath() { return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path; } protected function getUploadRootDir() { // the absolute directory path where uploaded // file should be saved return $this->basePath.$this->getUploadDir(); } protected function getUploadDir() { return 'uploads/fileItem'; } /** * @ORM\PostRemove */ public function postRemove(){ @unlink($this->path); } /** * @ORM\PrePersist * @ORM\PreUpdate */ public function upload() { $this->version++; $this->updateLastModified(); //if the original file was edited if($this->move == false && $this->update == true){ $this->originalHash = hash_file('sha256',$this->path); $this->size = (int)filesize($this->path); $this->mime = mime_content_type($this->path); } //if FileItem need to handle the storage of the given file if(is_null($this->file) && $this->move == true){ $path = $this->moveFile(); $this->path = realpath($path['save']).'/'.$path['newFileName']; $this->originalHash = hash_file('sha256',$this->path); $this->size = (int)filesize($this->path); $this->mime = mime_content_type($this->path); } if(is_null($this->file))//if its not from an upload we stop here return; //IF its an upload -> if ($this->path != $this->file->getClientOriginalName()) { $this->path = $this->file->getClientOriginalName(); } if (is_null($this->file)) { return; } $this->mime = $this->file->getMimeType(); $path = $this->moveFile(); $this->file = null; $this->path = realpath($path['save']).'/'.$path['newFileName']; $this->originalHash = hash_file('sha256',$this->path); } /** * @return array */ protected function moveFile(): array { $md5 = $this->generateHumanHash(64); $folderName = $result = substr($md5, 0, 4); $newFileName = time().$md5; $pathSave = $this->getUploadRootDir().'/'.$folderName; //idiotproof method ... $pathSave = str_replace('public_html/','',$pathSave); if (!is_dir($pathSave)) { mkdir($pathSave, 0775, true); } if (null === $this->file) { if(is_file($this->path)) @rename($this->path,realpath($pathSave).'/'.$newFileName); } else { $this->file->move( $pathSave, $newFileName ); } $this->move = false; return array('save'=>$pathSave,'newFileName' => $newFileName); } /** * @param int $length * @return bool|string */ private function generateHumanHash($length = 64){ $hash = ""; while(strlen($hash) < $length){ try { $hash .= hash('sha512', random_bytes($length)); } catch (\Exception $e) { $hash .= hash('sha512', mt_rand(0,$length*10000)); } } return substr($hash, 0,$length); } /** * Get id * * @return int */ public function getId() { return $this->id; } /** * Set name * * @param string $name * * @return FileItem */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set path * * @param string $path * * @return FileItem */ public function setPath($path) { $this->path = $path; return $this; } /** * Get path * * @return string */ public function getPath() { return $this->path; } /** * Set guid * * @param string $guid * * @return FileItem */ public function setGuid($guid) { $this->guid = $guid; return $this; } /** * Get guid * * @return string */ public function getGuid() { return $this->guid; } /** * @param int $permission * @return FileItem */ public function setPermission(int $permission): FileItem { $this->permission = $permission; return $this; } /** * @return int */ public function getPermission(): int { return $this->permission; } /** * @param FolderItem $parentFolder * @return FileItem */ public function setParentFolder(FolderItem $parentFolder): FileItem { if(!is_null($this->parentFolder)) $this->previousParentFolder = $this->parentFolder; $this->parentFolder = $parentFolder; return $this; } /** * @return FolderItem */ public function getParentFolder(): FolderItem { return $this->parentFolder; } /** * @param ArrayCollection $fileOwners * @return FileItem */ public function setFileOwners(ArrayCollection $fileOwners): FileItem { $this->fileOwners = $fileOwners; return $this; } /** * @return ArrayCollection */ public function getFileOwners(): ArrayCollection { return $this->fileOwners; } /** * @param FileOwner $user * @return $this */ public function addFileOwner(FileOwner $user) { $this->fileOwners[] = $user; return $this; } /** * @param FileOwner $user */ public function removeFileOwner(FileOwner $user) { $this->fileOwners->removeElement($user); } /** * @param null|string $basePath * @return FileItem */ public function setBasePath($basePath) { $this->basePath = $basePath; return $this; } /** * @return null|string */ public function getBasePath() { return $this->basePath; } /** * @return int */ public function getSize(): int { return $this->size; } /** * @param bool $inQueue * @return FileItem */ public function setInQueue(bool $inQueue): FileItem { $this->inQueue = $inQueue; return $this; } /** * @return bool */ public function isInQueue(): bool { return $this->inQueue; } /** * @return string */ public function getOriginalHash(): string { return $this->originalHash; } /** * @return \DateTime */ public function getDateUpd(): \DateTime { return $this->dateUpd; } /** * @return int */ public function getVersion(): int { return $this->version; } /** * @return string */ public function getMime(): string { return $this->mime; } /** * @return string */ public function getEtag(){ return md5($this->getName().(string)$this->getVersion()); } /** * @param array $attributes * @return FileItem */ public function setAttributes(array $attributes): FileItem { $this->attributes = $attributes; return $this; } /** * @param string $name * @param mixed $value * @return $this */ public function addAttribute($name,$value){ if(!is_array($this->attributes)) $this->attributes = array(); $this->attributes[$name] = $value; return $this; } /** * @param $name * @return $this */ public function removeAttribute($name){ if(isset($this->attributes[$name])) unset($this->attributes[$name]); return $this; } /** * @param $name * @param bool $default * @return bool|mixed */ public function getAttribute($name, $default = false){ if(isset($this->attributes[$name])) return $this->attributes[$name]; return $default; } /** * @return array */ public function getAttributes(): array { return $this->attributes; } /** * @param FilePreview $filePreview * @return FileItem */ public function setFilePreview(FilePreview $filePreview): FileItem { $this->filePreview = $filePreview; return $this; } /** * @return FilePreview|null */ public function getFilePreview() { return $this->filePreview; } /** * @param bool $static * @return FileItem */ public function setStatic(bool $static): FileItem { $this->static = $static; return $this; } /** * @return bool */ public function isStatic(): bool { return $this->static; } /** * @param \DateTime $lastModified * @return FileItem */ public function setLastModified(\DateTime $lastModified): FileItem { $this->lastModified = $lastModified; return $this; } public function updateLastModified(){ $this->lastModified = new \DateTime(); return $this; } /** * @return \DateTime|null */ public function getLastModified() { return $this->lastModified; } /** * @param string $mime * @return FileItem */ public function setMime(string $mime): FileItem { $this->mime = $mime; return $this; } /** * @param bool $hidden * @return FileItem */ public function setHidden(bool $hidden): FileItem { $this->hidden = $hidden; return $this; } /** * @return bool */ public function isHidden(): bool { return $this->hidden; } /** * @param int $size * @return FileItem */ public function setSize(int $size): FileItem { $this->size = $size; return $this; } /** * @param string $originalHash * @return FileItem */ public function setOriginalHash(string $originalHash): FileItem { $this->originalHash = $originalHash; return $this; } /** * @return bool */ public function isMove(): bool { return $this->move; } /** * @param bool $move * @return FileItem */ public function setMove(bool $move): FileItem { $this->move = $move; return $this; } /** * @return bool */ public function isUpdate(): bool { return $this->update; } /** * @param bool $update * @return FileItem */ public function setUpdate(bool $update): FileItem { $this->update = $update; return $this; } /** * @return FolderItem */ public function getPreviousParentFolder(): ?FolderItem { return $this->previousParentFolder; } /** * @param FolderItem|null $previousParentFolder * @return FileItem */ public function setPreviousParentFolder(?FolderItem $previousParentFolder): FileItem { $this->previousParentFolder = $previousParentFolder; return $this; } /** * @return bool */ public function isEncrypted(): bool { return $this->encrypted; } /** * @param bool $encrypted * @return FileItem */ public function setEncrypted(bool $encrypted): FileItem { $this->encrypted = $encrypted; return $this; } /** * @param Collection|null $fileGroupPermission * @return FileItem */ public function setFileGroupPermission(?Collection $fileGroupPermission): FileItem { $this->fileGroupPermission = $fileGroupPermission; return $this; } /** * @return Collection|null */ public function getFileGroupPermission(): ?Collection { return $this->fileGroupPermission; } /** * @param FileGroupPermission $fgp * @return $this */ public function addFileGroupPermission(FileGroupPermission $fgp) { $this->fileGroupPermission[] = $fgp; return $this; } /** * @param FileGroupPermission $fgp */ public function removeFileGroupPermission(FileGroupPermission $fgp) { $this->fileGroupPermission->removeElement($fgp); } }