68 lines
2.2 KiB
PHP
68 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Legacy;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class AvatarController extends Controller
|
|
{
|
|
public function show(Request $request, $id, $name = null)
|
|
{
|
|
$user_id = (int) $id;
|
|
|
|
// default avatar in project public gfx
|
|
$defaultAvatar = public_path('gfx/avatar.jpg');
|
|
|
|
try {
|
|
$icon = DB::connection('legacy')->table('users')->where('user_id', $user_id)->value('icon');
|
|
} catch (\Throwable $e) {
|
|
$icon = null;
|
|
}
|
|
|
|
$candidates = [];
|
|
if (!empty($icon)) {
|
|
// common legacy locations to check
|
|
$candidates[] = base_path('oldSite/www/files/usericons/' . $icon);
|
|
$candidates[] = base_path('oldSite/www/files/usericons/' . rawurlencode($icon));
|
|
$candidates[] = base_path('oldSite/www/files/usericons/' . basename($icon));
|
|
$candidates[] = public_path('avatar/' . $user_id . '/' . $icon);
|
|
$candidates[] = public_path('avatar/' . $user_id . '/' . basename($icon));
|
|
$candidates[] = storage_path('app/public/usericons/' . $icon);
|
|
$candidates[] = storage_path('app/public/usericons/' . basename($icon));
|
|
}
|
|
|
|
// find first readable file
|
|
$found = null;
|
|
foreach ($candidates as $path) {
|
|
if ($path && file_exists($path) && is_readable($path)) {
|
|
$found = $path;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($found) {
|
|
$type = @exif_imagetype($found);
|
|
if ($type) {
|
|
$mime = image_type_to_mime_type($type);
|
|
} else {
|
|
$f = finfo_open(FILEINFO_MIME_TYPE);
|
|
$mime = finfo_file($f, $found) ?: 'application/octet-stream';
|
|
finfo_close($f);
|
|
}
|
|
|
|
return response()->file($found, ['Content-Type' => $mime]);
|
|
}
|
|
|
|
// fallback to default
|
|
if (file_exists($defaultAvatar) && is_readable($defaultAvatar)) {
|
|
return response()->file($defaultAvatar, ['Content-Type' => 'image/jpeg']);
|
|
}
|
|
|
|
// final fallback: 404
|
|
abort(404);
|
|
}
|
|
}
|