feat: ds
This commit is contained in:
@@ -10,6 +10,7 @@ use App\Models\User;
|
||||
use App\Services\IngestService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Process;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
@@ -72,16 +73,16 @@ class DeviceAlbumStorageTest extends TestCase
|
||||
Http::assertSent(function ($request) {
|
||||
$text = (string) ($request->data()['text'] ?? '');
|
||||
|
||||
return str_contains($text, 'Sensitive Photo')
|
||||
return str_contains($text, '敏感照片')
|
||||
&& str_contains($text, 'dev-hit12')
|
||||
&& str_contains($text, 'x-hit</b>: 12');
|
||||
&& str_contains($text, '敏感分</b>: 12');
|
||||
});
|
||||
|
||||
$ingest->ingestPhotos($device, [$tmp2], ['x_hit' => Photo::X_HIT_ALERT]);
|
||||
$this->assertSame(1, collect(Http::recorded())->filter(function ($pair) {
|
||||
$text = (string) ($pair[0]->data()['text'] ?? '');
|
||||
|
||||
return str_contains($text, 'Sensitive Photo');
|
||||
return str_contains($text, '敏感照片');
|
||||
})->count());
|
||||
|
||||
@unlink($tmp);
|
||||
@@ -155,4 +156,78 @@ class DeviceAlbumStorageTest extends TestCase
|
||||
->postJson(route('user.devices.photos.clear', $other))
|
||||
->assertForbidden();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function photo_endpoint_serves_png_as_is(): void
|
||||
{
|
||||
Storage::fake('local');
|
||||
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
|
||||
$device = Device::query()->create(['device_id' => 'dev-png']);
|
||||
$tmp = sys_get_temp_dir().'/album_'.uniqid().'.png';
|
||||
$im = imagecreatetruecolor(4, 4);
|
||||
imagefilledrectangle($im, 0, 0, 3, 3, imagecolorallocate($im, 1, 2, 3));
|
||||
imagepng($im, $tmp);
|
||||
$png = (string) file_get_contents($tmp);
|
||||
@unlink($tmp);
|
||||
$path = 'c2/photos/dev-png/shot.png';
|
||||
Storage::disk('local')->put($path, $png);
|
||||
$photo = Photo::query()->create([
|
||||
'device_id' => $device->id,
|
||||
'sha256' => hash('sha256', $png),
|
||||
'path' => $path,
|
||||
'size' => strlen($png),
|
||||
]);
|
||||
|
||||
$this->actingAs($admin, 'admin')
|
||||
->get(route('admin.devices.photo', [$device, $photo->id]))
|
||||
->assertOk()
|
||||
->assertHeader('Content-Type', 'image/png')
|
||||
->assertSee($png, false);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function photo_endpoint_serves_heic_as_jpeg(): void
|
||||
{
|
||||
if (! is_executable('/usr/bin/sips')) {
|
||||
$this->markTestSkipped('sips is required to generate and convert HEIC');
|
||||
}
|
||||
Storage::fake('local');
|
||||
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
|
||||
$device = Device::query()->create(['device_id' => 'dev-heic']);
|
||||
|
||||
$jpg = sys_get_temp_dir().'/album_'.uniqid().'.jpg';
|
||||
$heicTmp = sys_get_temp_dir().'/album_'.uniqid().'.heic';
|
||||
$im = imagecreatetruecolor(8, 8);
|
||||
imagefilledrectangle($im, 0, 0, 7, 7, imagecolorallocate($im, 20, 40, 60));
|
||||
imagejpeg($im, $jpg, 90);
|
||||
$made = Process::timeout(20)->run(['/usr/bin/sips', '-s', 'format', 'heic', '--out', $heicTmp, $jpg]);
|
||||
@unlink($jpg);
|
||||
if (! $made->successful() || ! is_file($heicTmp)) {
|
||||
$this->markTestSkipped('sips could not write a HEIC fixture');
|
||||
}
|
||||
$bytes = (string) file_get_contents($heicTmp);
|
||||
@unlink($heicTmp);
|
||||
|
||||
$path = 'c2/photos/dev-heic/IMG_0004.HEIC';
|
||||
Storage::disk('local')->put($path, $bytes);
|
||||
$photo = Photo::query()->create([
|
||||
'device_id' => $device->id,
|
||||
'sha256' => hash('sha256', $bytes),
|
||||
'path' => $path,
|
||||
'size' => strlen($bytes),
|
||||
]);
|
||||
|
||||
$res = $this->actingAs($admin, 'admin')
|
||||
->get(route('admin.devices.photo', [$device, $photo->id]))
|
||||
->assertOk()
|
||||
->assertHeader('Content-Type', 'image/jpeg');
|
||||
$this->assertSame("\xFF\xD8", substr($res->getContent(), 0, 2));
|
||||
$this->assertSame($bytes, Storage::disk('local')->get($path));
|
||||
Storage::disk('local')->assertExists('c2/photo-previews/dev-heic/'.hash('sha256', $bytes).'.jpg');
|
||||
|
||||
$this->actingAs($admin, 'admin')
|
||||
->postJson(route('admin.devices.photos.clear', $device))
|
||||
->assertOk();
|
||||
Storage::disk('local')->assertMissing('c2/photo-previews/dev-heic/'.hash('sha256', $bytes).'.jpg');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user