/** * Upload provided file and create matching FileRecord object * * @param UploadedFile $file * @param $clientIp * @return FileRecord */ public function uploadFile(UploadedFile $file, $clientIp) { $extension = Str::lower($file->getClientOriginalExtension()); $generatedName = $this->generateName($extension); // Create SHA-256 hash of file $fileHash = hash_file('sha256', $file->getPathname()); // Check if file already exists $existingFile = FileRecord::where('hash', '=', $fileHash)->first(); if ($existingFile) { return $existingFile; } // Query previous scans in VirusTotal for this file if (config('virustotal.enabled') === true) { $this->checkVirusTotalForHash($fileHash); } // Get filesize $filesize = $file->getSize(); // Check max upload size $maxUploadSize = config('upload.max_size'); if ($filesize > $maxUploadSize) { throw new MaxUploadSizeException(); } // Move the file $uploadDirectory = config('upload.directory'); $file->move($uploadDirectory, $generatedName); // Create the record /** @var FileRecord $record */ $record = FileRecord::create(['client_name' => $file->getClientOriginalName(), 'generated_name' => $generatedName, 'filesize' => $filesize, 'hash' => $fileHash, 'uploaded_by_ip' => $clientIp]); return $record; }