file = $file; $this->fileName = $fileName; $this->fileExt = $fileExt; $this->size = $size; } /** * @param LaravelRequest $request * @param array $payload * @return FileUploadInfo|null * @throws ValidationException */ public static function build(LaravelRequest $request, array $payload):?FileUploadInfo { $file = null; $fileName = null; $fileExt = null; $size = 0; if($request->hasFile('file')) { Log::debug(sprintf("FileUploadInfo::build build file is present on request ( MULTIFORM )")); $file = $request->file('file'); // get in bytes should be converted to KB $size = $file->getSize(); if($size == 0) throw new ValidationException("File size is zero."); $fileName = $file->getClientOriginalName(); $fileExt = pathinfo($fileName, PATHINFO_EXTENSION); } if(is_null($file) && isset($payload['filepath'])){ Log::debug(sprintf("FileUploadInfo::build build file is present on as local storage (%s)", $payload['filepath'])); $disk = Storage::disk('local'); if(!$disk->exists($payload['filepath'])) throw new ValidationException(sprintf("file provide on filepath %s does not exists on local storage.", $payload['filepath'])); // get in bytes should be converted to KB $size = $disk->size($payload['filepath']); if($size == 0) throw new ValidationException("File size is zero."); $fileName = pathinfo($payload['filepath'],PATHINFO_BASENAME); $fileExt = pathinfo($fileName, PATHINFO_EXTENSION); $file = new UploadedFile($disk->path($payload['filepath']), $fileName); } if(is_null($file)) return null; $fileName = FileNameSanitizer::sanitize($fileName); return new self($file, $fileName, $fileExt, $size); } /** * @param string $unit * @return int|null */ public function getSize(string $unit = FileSizeUtil::Kb): ?int { if($unit === FileSizeUtil::Kb){ return $this->size / 1024; } return $this->size; } /** * @return UploadedFile */ public function getFile(): ?UploadedFile { return $this->file; } /** * @return string */ public function getFileName(): ?string { return $this->fileName; } /** * @return string */ public function getFileExt(): ?string { return $this->fileExt; } public function delete(){ if(is_null($this->file)) return; $realPath = $this->file->getRealPath(); Log::debug(sprintf("FileUploadInfo::delete deleting file %s", $realPath)); unlink($realPath); } }