Files
Gnuboard7/app/Http/Controllers/Api/Admin/AdminExtensionCustomAssetController.php
T
HeuJung 3945b6f1b3 feat(core,extensions): 구동 에셋 자체 제공 · 자산 실패 폴백 · 운영자 추가 에셋(custom/)
공개 저장소 이슈 gnuboard/g7 (@bigmsg) 제보에서 출발한 작업이다.

브라우저가 화면을 그리려고 제3자 CDN 에 도달해야 하면, 그 도달 실패는 예외도 로그도
남기지 않고 화면 기능만 조용히 사라진다. 폐쇄망·방화벽·광고차단기에서 재현되는데
자체 서버 로그에는 흔적이 없어 운영자가 원인을 특정할 수 없다. 제보된 것은 편집기
하나였지만 같은 구조가 아이콘·글꼴·코드편집기·압축 라이브러리·설치 마법사·개발
대시보드에 똑같이 있었으므로, 번들 확장과 템플릿 전체를 자체 제공으로 옮겼다.
런타임에 외부로 나가는 것은 주소 검색 서비스 하나만 남았다.

자체 제공만으로는 부족하다 — 자기 서버에서 받는 파일도 실패할 수 있고, 종전에는 그
실패가 무음이었다. CSS 경로에 재시도 계층을 세우고(스크립트 경로와 동형), 서버가 HTML
에 직접 심는 externals 까지 실패를 붙잡아 안내 배너와 [다시 시도]로 표면화했다.
편집기·코드편집기는 확보 실패 시 평문 입력으로 내려앉되 저장 계약을 유지한다.

두 번째 축은 운영자가 CSS 를 덧붙일 자리가 없던 문제다(sir.kr 문의). 확장 디렉토리의
custom/ 을 운영자 소유로 정해, 확장 교체가 그 디렉토리만은 보존하게 했다. 출처에
의존하지 않는 서술자로 해석하므로 나중에 다른 출처가 붙어도 소비자는 바뀌지 않는다.
확장 자산과 같은 메커니즘으로 정적 게시되어 CSS 내부 상대 url 도 해석되고, 파일을
고치면 그 변경을 감지해 재게시까지 예약된다.

FTP 접근이 없는 운영자에게는 그 자리도 없는 것과 같으므로 레이아웃 편집기에서 직접
넣고 고칠 수 있게 했다. 모듈·플러그인·템플릿이 한 엔드포인트를 공유한다 — 타입별로
나누면 같은 검증이 세 벌로 갈리고 그중 약한 하나가 우회로가 된다. 여기서 올린
스크립트는 그 레이아웃 한 장이 아니라 사이트 전 화면에서 실행되므로 레이아웃 편집과
분리된 전용 권한으로 연다. 운영자 CSS 가 화면을 조작 불능으로 만들면 그것을 고칠
화면에도 같은 CSS 가 실려 스스로 갇히므로, 서버가 목록을 비우는 탈출구(?custom=off)를
함께 뒀다.

동봉 자산은 재생성 경로에 버전 대조 가드를 붙였다. 선언과 다른 버전을 버전 디렉토리에
써 넣는 조용한 거짓말은 배포된 뒤에는 드러나지 않는다.
2026-08-27 16:47:14 +09:00

223 lines
8.8 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Admin;
use App\Exceptions\CustomAssetOperationException;
use App\Http\Controllers\Api\Base\AdminBaseController;
use App\Http\Requests\Admin\Extension\ReadExtensionCustomAssetRequest;
use App\Http\Requests\Admin\Extension\SaveExtensionCustomAssetRequest;
use App\Http\Requests\Admin\Extension\UploadExtensionCustomAssetRequest;
use App\Rules\AllowedTemplateFileType;
use App\Services\CustomAssetService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
/**
* 확장 사용자 추가 에셋(`custom/`) 어드민 컨트롤러
*
* 운영자가 자기 CSS·JS·폰트·이미지를 화면에서 직접 넣고 고칠 수 있게 한다. 레이아웃
* 편집기의 [커스텀 자산] 모달이 본 API 를 호출한다.
*
* 모듈·플러그인·템플릿을 **한 엔드포인트**가 다룬다. 타입별로 나누면 같은 검증·문서·테스트가
* 세 벌로 갈리고, 그중 하나만 약해지면 그 경로가 조용한 우회로가 된다. 기존
* `extensions/{type}/{identifier}` 선례(확장 복구 API)와 같은 형태다.
*
* 권한은 라우트의 permission 미들웨어(`core.extensions.custom_assets.manage`)가 담당한다.
* 레이아웃 편집 권한과 **분리**된 이유: 여기서 올린 스크립트는 그 레이아웃 한 장이 아니라
* 사이트 전 화면에서 실행되므로, 레이아웃을 고칠 수 있다는 것이 곧 그 권한이 될 수 없다.
*/
class AdminExtensionCustomAssetController extends AdminBaseController
{
/**
* 라우트 파라미터(단수) → 해석기 어휘(복수)
*
* 라우트는 기존 확장 공통 API 와 같은 단수형을 쓰고(`module|plugin|template`),
* 해석기·서빙은 디렉토리 이름과 같은 복수형을 쓴다. 변환을 한 곳에 모아 둔다 —
* 흩어지면 한쪽 표기만 고쳐 놓고 다른 쪽에서 조용히 빈 목록이 된다.
*/
private const TYPE_MAP = [
'module' => 'modules',
'plugin' => 'plugins',
'template' => 'templates',
];
public function __construct(
private CustomAssetService $service,
) {
parent::__construct();
}
/**
* 사용자 추가 에셋 목록 조회.
*
* @param string $type 확장 타입 (`module` | `plugin` | `template`)
* @param string $identifier 확장 식별자
* @return JsonResponse 파일 목록 + 편집기 메타(허용 확장자·크기 상한)
*/
public function index(string $type, string $identifier): JsonResponse
{
try {
$files = $this->service->list($this->resolveType($type), $identifier);
} catch (CustomAssetOperationException $e) {
return $this->error($e->errorKey, 422, null, $e->params);
} catch (\Throwable $e) {
Log::error('사용자 추가 에셋 목록 조회 실패', [
'type' => $type,
'identifier' => $identifier,
'error' => $e->getMessage(),
]);
return $this->error('custom_assets.errors.read_failed', 500, $e, ['path' => 'custom/']);
}
return $this->success('custom_assets.messages.listed', [
'files' => $files,
'editable_extensions' => CustomAssetService::EDITABLE_EXTENSIONS,
'uploadable_extensions' => AllowedTemplateFileType::getAllowedExtensions(),
'max_text_bytes' => CustomAssetService::MAX_TEXT_BYTES,
'max_upload_bytes' => CustomAssetService::MAX_UPLOAD_BYTES,
]);
}
/**
* 텍스트 파일 본문 조회.
*
* @param ReadExtensionCustomAssetRequest $request 검증된 요청 (`path`)
* @param string $type 확장 타입
* @param string $identifier 확장 식별자
* @return JsonResponse 본문 응답
*/
public function show(ReadExtensionCustomAssetRequest $request, string $type, string $identifier): JsonResponse
{
$path = (string) $request->validated('path');
try {
$file = $this->service->read($this->resolveType($type), $identifier, $path);
} catch (CustomAssetOperationException $e) {
return $this->error($e->errorKey, 422, null, $e->params);
} catch (\Throwable $e) {
Log::error('사용자 추가 에셋 본문 조회 실패', [
'type' => $type,
'identifier' => $identifier,
'error' => $e->getMessage(),
]);
return $this->error('custom_assets.errors.read_failed', 500, $e, ['path' => $path]);
}
return $this->success('custom_assets.messages.listed', $file);
}
/**
* 텍스트 파일 본문 저장 (없으면 생성).
*
* @param SaveExtensionCustomAssetRequest $request 검증된 요청 (`path`, `content`)
* @param string $type 확장 타입
* @param string $identifier 확장 식별자
* @return JsonResponse 저장 결과
*/
public function store(SaveExtensionCustomAssetRequest $request, string $type, string $identifier): JsonResponse
{
$path = (string) $request->validated('path');
try {
$saved = $this->service->save(
$this->resolveType($type),
$identifier,
$path,
(string) $request->validated('content'),
);
} catch (CustomAssetOperationException $e) {
return $this->error($e->errorKey, 422, null, $e->params);
} catch (\Throwable $e) {
Log::error('사용자 추가 에셋 저장 실패', [
'type' => $type,
'identifier' => $identifier,
'error' => $e->getMessage(),
]);
return $this->error('custom_assets.errors.write_failed', 500, $e, ['path' => $path]);
}
return $this->success('custom_assets.messages.saved', $saved);
}
/**
* 파일 업로드 (폰트·이미지 등 바이너리 포함).
*
* @param UploadExtensionCustomAssetRequest $request 검증된 요청 (`file`, `directory`)
* @param string $type 확장 타입
* @param string $identifier 확장 식별자
* @return JsonResponse 업로드 결과
*/
public function upload(UploadExtensionCustomAssetRequest $request, string $type, string $identifier): JsonResponse
{
$directory = $request->validated('directory');
try {
$uploaded = $this->service->upload(
$this->resolveType($type),
$identifier,
$request->file('file'),
is_string($directory) ? $directory : null,
);
} catch (CustomAssetOperationException $e) {
return $this->error($e->errorKey, 422, null, $e->params);
} catch (\Throwable $e) {
Log::error('사용자 추가 에셋 업로드 실패', [
'type' => $type,
'identifier' => $identifier,
'error' => $e->getMessage(),
]);
return $this->error('custom_assets.errors.write_failed', 500, $e, ['path' => (string) $directory]);
}
return $this->success('custom_assets.messages.uploaded', $uploaded);
}
/**
* 파일 삭제.
*
* @param ReadExtensionCustomAssetRequest $request 검증된 요청 (`path`)
* @param string $type 확장 타입
* @param string $identifier 확장 식별자
* @return JsonResponse 삭제 결과
*/
public function destroy(ReadExtensionCustomAssetRequest $request, string $type, string $identifier): JsonResponse
{
$path = (string) $request->validated('path');
try {
$this->service->delete($this->resolveType($type), $identifier, $path);
} catch (CustomAssetOperationException $e) {
return $this->error($e->errorKey, 422, null, $e->params);
} catch (\Throwable $e) {
Log::error('사용자 추가 에셋 삭제 실패', [
'type' => $type,
'identifier' => $identifier,
'error' => $e->getMessage(),
]);
return $this->error('custom_assets.errors.delete_failed', 500, $e, ['path' => $path]);
}
return $this->success('custom_assets.messages.deleted', ['path' => $path]);
}
/**
* 라우트 타입 파라미터를 해석기 어휘로 바꿉니다.
*
* 라우트 정규식이 이미 세 값으로 제한하지만, 매핑에 없으면 그대로 넘긴다 —
* 해석기가 알 수 없는 타입을 무효로 판정해 422 를 만든다(빈 목록으로 조용히
* 통과시키지 않는다).
*
* @param string $type 라우트 파라미터
* @return string 해석기 어휘
*/
private function resolveType(string $type): string
{
return self::TYPE_MAP[$type] ?? $type;
}
}