BookStack\Services\AttachmentService::putFileInStorage PHP Method

putFileInStorage() protected method

Store a file in storage with the given filename
protected putFileInStorage ( $attachmentName, Symfony\Component\HttpFoundation\File\UploadedFile $uploadedFile ) : string
$attachmentName
$uploadedFile Symfony\Component\HttpFoundation\File\UploadedFile
return string
    protected function putFileInStorage($attachmentName, UploadedFile $uploadedFile)
    {
        $attachmentData = file_get_contents($uploadedFile->getRealPath());
        $storage = $this->getStorage();
        $attachmentBasePath = 'uploads/files/' . Date('Y-m-M') . '/';
        $storageBasePath = $this->getStorageBasePath() . $attachmentBasePath;
        $uploadFileName = $attachmentName;
        while ($storage->exists($storageBasePath . $uploadFileName)) {
            $uploadFileName = str_random(3) . $uploadFileName;
        }
        $attachmentPath = $attachmentBasePath . $uploadFileName;
        $attachmentStoragePath = $this->getStorageBasePath() . $attachmentPath;
        try {
            $storage->put($attachmentStoragePath, $attachmentData);
        } catch (Exception $e) {
            throw new FileUploadException('File path ' . $attachmentStoragePath . ' could not be uploaded to. Ensure it is writable to the server.');
        }
        return $attachmentPath;
    }