guid = $this->generateToken(128); $this->name = $name; $this->originalPath = $originalPath; $this->size = filesize($originalPath); $this->mime = mime_content_type ($originalPath); $this->version = 0; $this->static = false; $this->path = ''; $this->originalHash = hash_file('sha256',$originalPath); $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->encrypted = false; $this->fileOwners = new ArrayCollection(); $this->box = $box; $this->parentFolder = $parentFolder; } /** * @param int $length * @return bool|string */ private function generateToken($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); } // /** // * 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 $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); // } /** * Get id * * @return int */ public function getId() { return $this->id; } public function getName(): ?string { return $this->name; } public function setName(string $name): self { $this->name = $name; return $this; } public function getMime(): ?string { return $this->mime; } public function setMime(string $mime): self { $this->mime = $mime; return $this; } private function generateMainMine(){ switch ($this->mime){ case 'image/jpeg': case 'image/gif': case 'image/png': case 'image/tiff': case 'image/svg+xml': return 'image'; break; case 'video/x-msvideo': case 'video/mpeg': case 'video/3gpp': case 'video/3gpp2': case 'video/ogg': return 'video'; break; case 'application/pdf': return 'pdf'; break; default: $temp = explode('/',$this->mime); return $temp[0]; } return ''; } public function getVersion(): ?int { return $this->version; } public function setVersion(int $version): self { $this->version = $version; return $this; } public function getPath(): ?string { return $this->path; } public function setPath(string $path): self { $this->path = $path; return $this; } public function getGuid(): ?string { return $this->guid; } public function setGuid(string $guid): self { $this->guid = $guid; return $this; } public function getOriginalHash(): ?string { return $this->originalHash; } public function setOriginalHash(string $originalHash): self { $this->originalHash = $originalHash; return $this; } public function getSize(): ?int { return $this->size; } public function setSize(int $size): self { $this->size = $size; return $this; } public function getDateUpd(): ?\DateTimeInterface { return $this->dateUpd; } public function setDateUpd(?\DateTimeInterface $dateUpd): self { $this->dateUpd = $dateUpd; return $this; } public function getLastModified(): ?\DateTimeInterface { return $this->lastModified; } public function setLastModified(?\DateTimeInterface $lastModified): self { $this->lastModified = $lastModified; return $this; } public function getInQueue(): ?bool { return $this->inQueue; } public function setInQueue(bool $inQueue): self { $this->inQueue = $inQueue; return $this; } public function getPermission(): ?int { return $this->permission; } public function setPermission(int $permission): self { $this->permission = $permission; return $this; } public function getStatic(): ?bool { return $this->static; } public function setStatic(bool $static): self { $this->static = $static; return $this; } public function getEncrypted(): ?bool { return $this->encrypted; } public function setEncrypted(bool $encrypted): self { $this->encrypted = $encrypted; return $this; } public function getHidden(): ?bool { return $this->hidden; } public function setHidden(bool $hidden): self { $this->hidden = $hidden; return $this; } public function getAttributes(): ?array { return $this->attributes; } public function setAttributes(array $attributes): self { $this->attributes = $attributes; return $this; } /** * @return Collection|FileOwner[] */ public function getFileOwners(): Collection { return $this->fileOwners; } public function addFileOwner(FileOwner $fileOwner): self { if (!$this->fileOwners->contains($fileOwner)) { $this->fileOwners[] = $fileOwner; $fileOwner->setFile($this); } return $this; } public function removeFileOwner(FileOwner $fileOwner): self { if ($this->fileOwners->contains($fileOwner)) { $this->fileOwners->removeElement($fileOwner); // set the owning side to null (unless already changed) if ($fileOwner->getFile() === $this) { $fileOwner->setFile(null); } } return $this; } /** * @return Collection|FileGroupPermission[] */ public function getFileGroupPermission(): Collection { return $this->fileGroupPermission; } public function addFileGroupPermission(FileGroupPermission $fileGroupPermission): self { if (!$this->fileGroupPermission->contains($fileGroupPermission)) { $this->fileGroupPermission[] = $fileGroupPermission; $fileGroupPermission->setFile($this); } return $this; } public function removeFileGroupPermission(FileGroupPermission $fileGroupPermission): self { if ($this->fileGroupPermission->contains($fileGroupPermission)) { $this->fileGroupPermission->removeElement($fileGroupPermission); // set the owning side to null (unless already changed) if ($fileGroupPermission->getFile() === $this) { $fileGroupPermission->setFile(null); } } return $this; } public function getFilePreview(): ?FilePreview { return $this->filePreview; } public function setFilePreview(?FilePreview $filePreview): self { $this->filePreview = $filePreview; return $this; } public function getParentFolder(): ?FolderItemInterface { return $this->parentFolder; } public function setParentFolder(?FolderItemInterface $parentFolder): self { $this->parentFolder = $parentFolder; return $this; } public function getPreviousParentFolder(): ?FolderItemInterface { return $this->previousParentFolder; } public function setPreviousParentFolder(?FolderItemInterface $previousParentFolder): self { $this->previousParentFolder = $previousParentFolder; return $this; } /** * @return string */ public function getOriginalPath(): string { return $this->originalPath; } /** * @param string $originalPath */ public function setOriginalPath(string $originalPath): void { $this->originalPath = $originalPath; } /** * @return string|null */ public function getBasePath(): ?string { return $this->basePath; } /** * @param string|null $basePath */ public function setBasePath(?string $basePath): void { $this->basePath = $basePath; } /** * @return bool */ public function isUpdate(): bool { return $this->update; } /** * @param bool $update */ public function setUpdate(bool $update): void { $this->update = $update; } public function getDownloadPath(){ return $this->path.$this->guid; } public function getFileItemSymlink(): ?string { return $this->fileItemSymlink; } public function setFileItemSymlink(string $fileItemSymlink): self { $this->fileItemSymlink = $fileItemSymlink; return $this; } /** * @return string */ public function getImagePreview(): string { return $this->imagePreview; } /** * @param string $imagePreview * @return FileItem */ public function setImagePreview(string $imagePreview): FileItem { $this->imagePreview = $imagePreview; return $this; } public function getThumbnail(): ?string { return $this->thumbnail; } public function setThumbnail(?string $thumbnail): self { $this->thumbnail = $thumbnail; return $this; } /** * @return string */ public function getMainMime(): string { return $this->generateMainMine(); } public function getBox(): ?Box { return $this->box; } public function setBox(?Box $box): self { $this->box = $box; return $this; } public function getTrashBox(): ?TrashBox { return $this->trashBox; } public function setTrashBox(?TrashBox $trashBox): self { $this->trashBox = $trashBox; return $this; } public function getPhotoBox(): ?PhotosBox { return $this->photoBox; } public function setPhotoBox(?PhotosBox $photoBox): self { $this->photoBox = $photoBox; return $this; } }