-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathFileProvider.php
More file actions
178 lines (154 loc) · 5.85 KB
/
FileProvider.php
File metadata and controls
178 lines (154 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Tests\Helpers;
use BookStack\Entities\Models\Page;
use BookStack\Uploads\Attachment;
use BookStack\Uploads\AttachmentService;
use Illuminate\Http\UploadedFile;
use Illuminate\Testing\TestResponse;
use stdClass;
use Tests\TestCase;
class FileProvider
{
/**
* Get the path to a file in the test-data-directory.
*/
public function testFilePath(string $fileName): string
{
return base_path('tests/test-data/' . $fileName);
}
/**
* Creates a new temporary image file using the given name,
* with the content decoded from the given bas64 file name.
* Is generally used for testing sketchy files that could trip AV.
*/
public function imageFromBase64File(string $base64FileName, string $imageFileName): UploadedFile
{
$imagePath = implode(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), $imageFileName]);
$base64FilePath = $this->testFilePath($base64FileName);
$data = file_get_contents($base64FilePath);
$decoded = base64_decode($data);
file_put_contents($imagePath, $decoded);
return new UploadedFile($imagePath, $imageFileName, 'image/png', null, true);
}
/**
* Get a test image UploadedFile instance, that can be uploaded via test requests.
*/
public function uploadedImage(string $fileName, string $testDataFileName = ''): UploadedFile
{
return new UploadedFile($this->testFilePath($testDataFileName ?: 'test-image.png'), $fileName, 'image/png', null, true);
}
/**
* Get a test txt UploadedFile instance, that can be uploaded via test requests.
*/
public function uploadedTextFile(string $fileName): UploadedFile
{
return new UploadedFile($this->testFilePath('test-file.txt'), $fileName, 'text/plain', null, true);
}
/**
* Get raw data for a PNG image test file.
*/
public function pngImageData(): string
{
return file_get_contents($this->testFilePath('test-image.png'));
}
/**
* Get raw data for a Jpeg image test file.
*/
public function jpegImageData(): string
{
return file_get_contents($this->testFilePath('test-image.jpg'));
}
/**
* Get the expected relative path for an uploaded image of the given type and filename.
*/
public function expectedImagePath(string $imageType, string $fileName): string
{
return '/uploads/images/' . $imageType . '/' . date('Y-m') . '/' . $fileName;
}
/**
* Performs an image gallery upload request with the given name.
*/
public function uploadGalleryImage(TestCase $case, string $name, int $uploadedTo = 0, string $contentType = 'image/png', string $testDataFileName = ''): TestResponse
{
$file = $this->uploadedImage($name, $testDataFileName);
return $case->call('POST', '/images/gallery', ['uploaded_to' => $uploadedTo], [], ['file' => $file], ['CONTENT_TYPE' => $contentType]);
}
/**
* Upload a new gallery image and return a set of details about the image,
* including the json decoded response of the upload.
* Ensures the upload succeeds.
*
* @return array{name: string, path: string, page: Page, response: stdClass}
*/
public function uploadGalleryImageToPage(TestCase $case, Page $page, string $testDataFileName = ''): array
{
$imageName = $testDataFileName ?: 'first-image.png';
$relPath = $this->expectedImagePath('gallery', $imageName);
$this->deleteAtRelativePath($relPath);
$upload = $this->uploadGalleryImage($case, $imageName, $page->id, 'image/png', $testDataFileName);
$upload->assertStatus(200);
return [
'name' => $imageName,
'path' => $relPath,
'page' => $page,
'response' => json_decode($upload->getContent()),
];
}
/**
* Uploads an attachment file with the given name.
*/
public function uploadAttachmentFile(TestCase $case, string $name, int $uploadedTo = 0): TestResponse
{
$file = $this->uploadedTextFile($name);
return $case->call('POST', '/attachments/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
}
/**
* Upload a new attachment from the given raw data of the given type, to the given page.
* Returns the attachment
*/
public function uploadAttachmentDataToPage(TestCase $case, Page $page, string $filename, string $content, string $mimeType): Attachment
{
$file = tmpfile();
$filePath = stream_get_meta_data($file)['uri'];
file_put_contents($filePath, $content);
$upload = new UploadedFile($filePath, $filename, $mimeType, null, true);
$case->call('POST', '/attachments/upload', ['uploaded_to' => $page->id], [], ['file' => $upload], []);
return $page->attachments()->where('uploaded_to', '=', $page->id)->latest()->firstOrFail();
}
/**
* Delete an uploaded image.
*/
public function deleteAtRelativePath(string $path): void
{
$fullPath = $this->relativeToFullPath($path);
if (file_exists($fullPath)) {
unlink($fullPath);
}
}
/**
* Convert a relative path used by default in this provider to a full
* absolute local filesystem path.
*/
public function relativeToFullPath(string $path): string
{
return public_path($path);
}
/**
* Delete all uploaded files.
* To assist with cleanup.
*/
public function deleteAllAttachmentFiles(): void
{
$fileService = app()->make(AttachmentService::class);
foreach (Attachment::all() as $file) {
$fileService->deleteFile($file);
}
}
/**
* Reset the application favicon image in the public path.
*/
public function resetAppFavicon(): void
{
file_put_contents(public_path('favicon.ico'), file_get_contents(public_path('icon.ico')));
}
}