Skip to content

Commit 97d097e

Browse files
WIP: Fixture for document seeding
1 parent dd6f4f9 commit 97d097e

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

database/factories/MediaFactory.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,31 @@ public function image(string $key): Factory
3030
});
3131
}
3232

33-
public function withFile(?string $fileName = null)
33+
public function file(string $key): Factory
3434
{
35-
return $this->state(function (array $attributes) use ($fileName) {
36-
$fileName = $fileName ?: fake()->slug() . '.jpg';
37-
$path = $this->getRandomFixtureImagePath();
35+
return $this
36+
->state(function (array $attributes) use ($key) {
37+
return [
38+
'model_key' => $key,
39+
'file_name' => sprintf('data/files/%s.jpg', $this->faker->unique()->slug()),
40+
'mime_type' => 'image/jpeg',
41+
'disk' => 'local',
42+
'size' => $this->faker->numberBetween(100, 100000),
43+
];
44+
});
45+
}
46+
47+
public function withFile(?string $fileName = null, string $type="image")
48+
{
49+
return $this->state(function (array $attributes) use ($fileName,$type) {
50+
$fileName = $fileName ?: fake()->slug() . ($type==='image'?'.jpg':'.pdf');
51+
$path = ($type==="image" ? $this->getRandomFixtureImagePath() : $this->getRandomFixtureDocumentPath());
3852

3953
Storage::disk('local')
40-
->put("/data/medias/$fileName", file_get_contents($path));
54+
->put("/data/".($type==='image'?'medias':'files')."/$fileName", file_get_contents($path));
4155

4256
return [
43-
'file_name' => "data/medias/$fileName",
57+
'file_name' => "data/".($type==='image'?'medias':'files')."/$fileName",
4458
];
4559
});
4660
}
@@ -54,4 +68,14 @@ private function getRandomFixtureImagePath(): string
5468
)
5569
);
5670
}
71+
72+
private function getRandomFixtureDocumentPath(): string
73+
{
74+
return base_path(
75+
sprintf(
76+
'vendor/code16/ozu-client/database/fixtures/documents/%s.pdf',
77+
rand(1, 26)
78+
)
79+
);
80+
}
5781
}

database/fixtures/documents/doc.pdf

30.5 KB
Binary file not shown.

src/Eloquent/Media.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function humanReadableSize($precision = 2): ?string
5151
$base = log($size) / log(1024);
5252
$suffixes = array(' bytes', ' KB', ' MB', ' GB', ' TB');
5353

54-
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
54+
return $this->size === 0 ? '0 bytes' : (round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)]);
5555
} else {
5656
return $this->size;
5757
}

0 commit comments

Comments
 (0)