feat(ckeditor5,board,ecommerce,page,core,admin_basic): 미참조 에디터 업로드 이미지 정리 신설 + 업로드 고아 동종 결함 7건 전수 해소
공개 (@Tuwasduliebst 제보) 대응. - ckeditor5: 미참조 이미지 정리 엔진 신설 — 참조 판정(hash + 파일명 2토큰, filter_reference_sources 훅으로 확장 콘텐츠 등록) + 정리 커맨드(기본 꺼짐 옵트인, 3중 게이트) + 업로드 관리 화면(권한 분리·참조 배지·개별/선택 삭제) + 기설치본 설정 백필 upgrade step - board: 임시 첨부 GC(상시) + 소프트삭제 첨부 영구 정리(옵트인) + 설정 UI, 저장 payload 누락(F4)·빈 temp 디렉토리 잔존(F5) 수정 - ecommerce: 임시 상품 이미지 GC + 카테고리 삭제 시 이미지 파일 미삭제(S4) 수정 - page: 임시 첨부 GC - 코어: 고아 첨부 정리 커맨드(옵트인, site_logo 보호) + 사이트 로고 교체 시 구 로고 재편입 차단(S6) + DB 백업 미구현 501 정직화(B3) + 플러그인 관리자 메뉴 설치·업데이트 자동 동기화(F1·F7) - 동반: 플러그인 설정 검증 오류 필드명 라벨화(F3), Toggle 노드 type 정정 수평 전개(marketing 1.0.2 bump·gdpr·ecommerce), ja 언어팩 4종, API 문서, hooks.md, 시나리오 매니페스트 8종, Playwright E2E 4종
This commit is contained in:
1 parent
1d895a6428
commit
cfc2caba1b
170 files changed
+11142
-209
No files matched your search
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Sirsoft\Board\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Modules\Sirsoft\Board\Services\AttachmentService;
|
||||
|
||||
/**
|
||||
* 게시판 첨부파일 정리 커맨드
|
||||
*
|
||||
* 두 파트를 순차 실행합니다.
|
||||
*
|
||||
* 1. 임시 첨부 정리 (항상 실행) — 글쓰기 폼에서 올린 뒤 저장 없이 이탈해 남은 첨부.
|
||||
* `temp_key` 가 남아 있으면 끝내 연결되지 않은 폼 세션 부산물이라 오탐 여지가 없다.
|
||||
* 2. 소프트 삭제 첨부 영구 정리 (운영자 옵트인) — 삭제된 첨부는 휴지통 복원 대비로
|
||||
* 파일이 남는다. 사용자 파일을 실제로 파기하므로 기본 꺼짐이며, 스케줄 호출
|
||||
* (`--scheduled`)에서 설정을 false 폴백으로 확인한 뒤에만 수행한다.
|
||||
*
|
||||
* 토글이 막는 것은 **자동(스케줄) 실행**이다. `--scheduled` 없이 서버에서 직접 실행하면
|
||||
* 토글과 무관하게 파기한다 — 운영자가 의도해서 친 명령이기 때문이다.
|
||||
*
|
||||
* @example php artisan sirsoft-board:prune-attachments --dry-run
|
||||
* @example php artisan sirsoft-board:prune-attachments --temp-days=1
|
||||
*/
|
||||
class PruneAttachmentsCommand extends Command
|
||||
{
|
||||
/**
|
||||
* 커맨드 이름 및 시그니처
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'sirsoft-board:prune-attachments
|
||||
{--dry-run : 실제 삭제 없이 대상 건수만 확인}
|
||||
{--limit=500 : 한 번에 처리할 최대 건수}
|
||||
{--temp-days=2 : 임시 첨부 보존기간(일)}
|
||||
{--purge-days= : 소프트 삭제 첨부 보존기간(일) 재정의 — 미지정 시 모듈 설정값}
|
||||
{--scheduled : 스케줄러 호출 표시 — 영구 정리 토글이 꺼져 있으면 그 파트만 건너뜀 (이 옵션 없이 직접 실행하면 토글과 무관하게 파기)}';
|
||||
|
||||
/**
|
||||
* 커맨드 설명
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '방치된 임시 첨부와 보존기간이 지난 삭제 첨부를 정리합니다.';
|
||||
|
||||
/**
|
||||
* @param AttachmentService $attachmentService 첨부 서비스
|
||||
*/
|
||||
public function __construct(
|
||||
protected AttachmentService $attachmentService
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* 커맨드 실행
|
||||
*
|
||||
* @return int 종료 코드
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$limit = max(1, (int) $this->option('limit'));
|
||||
$isDryRun = (bool) $this->option('dry-run');
|
||||
|
||||
$this->pruneTemp($limit, $isDryRun);
|
||||
$this->purgeSoftDeleted($limit, $isDryRun);
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 임시 첨부 정리 파트 (항상 실행).
|
||||
*
|
||||
* @param int $limit 최대 처리 건수
|
||||
* @param bool $isDryRun 판정만 수행할지 여부
|
||||
*/
|
||||
private function pruneTemp(int $limit, bool $isDryRun): void
|
||||
{
|
||||
$days = (int) $this->option('temp-days');
|
||||
|
||||
// 보존기간 0 이하 = 방금 올린 임시 첨부까지 지우게 되므로 차단한다.
|
||||
if ($days < 1) {
|
||||
$this->info('임시 첨부 보존기간이 1일 미만이어서 임시 첨부 정리를 수행하지 않았습니다.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$result = $this->attachmentService->pruneTempUploads($days, $limit, $isDryRun);
|
||||
|
||||
if ($isDryRun) {
|
||||
$this->info("[DRY RUN] 보존기간({$days}일) 경과 미연결 임시 첨부: {$result['scanned']}건");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->info(sprintf(
|
||||
'보존기간(%d일) 경과 임시 첨부 %d건 중 %d건을 삭제했습니다. (실패 %d건)',
|
||||
$days,
|
||||
$result['scanned'],
|
||||
$result['deleted'],
|
||||
$result['failed'],
|
||||
));
|
||||
|
||||
Log::info('PruneAttachmentsCommand: 임시 첨부 정리 완료', [
|
||||
'days' => $days,
|
||||
'limit' => $limit,
|
||||
] + $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 소프트 삭제 첨부 영구 정리 파트 (운영자 옵트인).
|
||||
*
|
||||
* @param int $limit 최대 처리 건수
|
||||
* @param bool $isDryRun 판정만 수행할지 여부
|
||||
*/
|
||||
private function purgeSoftDeleted(int $limit, bool $isDryRun): void
|
||||
{
|
||||
if ($this->option('scheduled') && ! $this->isPurgeEnabled()) {
|
||||
$this->info('삭제 첨부 영구 정리가 꺼져 있어 실행하지 않았습니다. (attachment_settings.purge_enabled = false)');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$days = $this->resolvePurgeDays();
|
||||
|
||||
if ($days < 1) {
|
||||
$this->info('삭제 첨부 보존기간이 1일 미만이어서 영구 정리를 수행하지 않았습니다.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$result = $this->attachmentService->purgeSoftDeleted($days, $limit, $isDryRun);
|
||||
|
||||
if ($isDryRun) {
|
||||
$this->info("[DRY RUN] 보존기간({$days}일) 경과 삭제 첨부: {$result['scanned']}건");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->info(sprintf(
|
||||
'보존기간(%d일) 경과 삭제 첨부 %d건 중 %d건을 영구 삭제했습니다. (실패 %d건)',
|
||||
$days,
|
||||
$result['scanned'],
|
||||
$result['deleted'],
|
||||
$result['failed'],
|
||||
));
|
||||
|
||||
Log::info('PruneAttachmentsCommand: 삭제 첨부 영구 정리 완료', [
|
||||
'days' => $days,
|
||||
'limit' => $limit,
|
||||
] + $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 영구 정리 토글을 false 폴백으로 조회합니다.
|
||||
*
|
||||
* @return bool 영구 정리 활성 여부
|
||||
*/
|
||||
private function isPurgeEnabled(): bool
|
||||
{
|
||||
return (bool) module_setting('sirsoft-board', 'attachment_settings.purge_enabled', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 영구 정리 보존기간을 해석합니다 (옵션 > 모듈 설정 > 기본 30일).
|
||||
*
|
||||
* @return int 보존기간(일)
|
||||
*/
|
||||
private function resolvePurgeDays(): int
|
||||
{
|
||||
$option = $this->option('purge-days');
|
||||
|
||||
if ($option !== null && $option !== '') {
|
||||
return (int) $option;
|
||||
}
|
||||
|
||||
return (int) module_setting('sirsoft-board', 'attachment_settings.purge_retention_days', 30);
|
||||
}
|
||||
}
|
||||
+8
-1
@@ -58,6 +58,7 @@ class StoreBoardSettingsRequest extends FormRequest
|
||||
'spam_security.comment_cooldown_seconds',
|
||||
'spam_security.report_cooldown_seconds',
|
||||
'spam_security.view_count_cache_ttl',
|
||||
'attachment_settings.purge_retention_days',
|
||||
];
|
||||
|
||||
$booleanFields = [
|
||||
@@ -73,6 +74,7 @@ class StoreBoardSettingsRequest extends FormRequest
|
||||
'seo.seo_boards',
|
||||
'seo.seo_board',
|
||||
'seo.seo_post_detail',
|
||||
'attachment_settings.purge_enabled',
|
||||
];
|
||||
|
||||
$data = $this->all();
|
||||
@@ -262,6 +264,11 @@ class StoreBoardSettingsRequest extends FormRequest
|
||||
'seo.seo_boards' => ['nullable', 'boolean'],
|
||||
'seo.seo_board' => ['nullable', 'boolean'],
|
||||
'seo.seo_post_detail' => ['nullable', 'boolean'],
|
||||
|
||||
// 첨부 정리 정책 — 사용자 파일을 파기하므로 보존기간 하한을 서버가 강제한다.
|
||||
// 상한(3650일)은 "사실상 정리하지 않음" 과 구분되지 않는 값을 저장하지 않기 위함이다.
|
||||
'attachment_settings.purge_enabled' => ['nullable', 'boolean'],
|
||||
'attachment_settings.purge_retention_days' => ['nullable', 'integer', 'min:'.$limits['attachment_purge_retention_days_min'], 'max:'.$limits['attachment_purge_retention_days_max']],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -311,7 +318,7 @@ class StoreBoardSettingsRequest extends FormRequest
|
||||
public function validatedSettings(): array
|
||||
{
|
||||
$validated = $this->validated();
|
||||
$validCategories = ['basic_defaults', 'report_policy', 'spam_security', 'display', 'seo', 'notifications'];
|
||||
$validCategories = ['basic_defaults', 'report_policy', 'spam_security', 'display', 'seo', 'notifications', 'attachment_settings'];
|
||||
|
||||
return array_filter(
|
||||
$validated,
|
||||
|
||||
@@ -67,6 +67,9 @@ trait ReadsBoardLimits
|
||||
|
||||
'new_display_hours_min' => 0,
|
||||
'new_display_hours_max' => 720,
|
||||
|
||||
'attachment_purge_retention_days_min' => 1,
|
||||
'attachment_purge_retention_days_max' => 3650,
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Sirsoft\Board\Listeners;
|
||||
|
||||
use App\Contracts\Extension\HookListenerInterface;
|
||||
|
||||
/**
|
||||
* 게시판 본문을 CKEditor5 이미지 참조 스캔 대상으로 등록하는 리스너
|
||||
*
|
||||
* `sirsoft-ckeditor5.image.filter_reference_sources` 필터 훅에 게시글 본문 컬럼을
|
||||
* 덧붙입니다. 등록하지 않으면 게시글에만 쓰이는 이미지가 "미참조" 로 판정돼
|
||||
* 자동 정리 대상이 됩니다.
|
||||
*
|
||||
* 테이블명 문자열만 덧붙이며 DB 에 접근하지 않습니다 — 실제 조회와 존재 검증은
|
||||
* 플러그인의 참조 판정 서비스가 수행합니다.
|
||||
*/
|
||||
class Ckeditor5ReferenceSourcesListener implements HookListenerInterface
|
||||
{
|
||||
/**
|
||||
* 구독할 훅 목록 반환
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function getSubscribedHooks(): array
|
||||
{
|
||||
return [
|
||||
'sirsoft-ckeditor5.image.filter_reference_sources' => [
|
||||
'method' => 'addBoardSources',
|
||||
'priority' => 10,
|
||||
'type' => 'filter',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 훅 이벤트를 처리합니다.
|
||||
*
|
||||
* Filter 훅은 getSubscribedHooks 에서 지정한 메서드를 직접 호출하므로
|
||||
* 이 메서드는 인터페이스 요구사항 충족을 위해서만 존재합니다.
|
||||
*
|
||||
* @param mixed ...$args 훅에서 전달된 인수들
|
||||
*/
|
||||
public function handle(...$args): void {}
|
||||
|
||||
/**
|
||||
* 게시판 참조 소스를 추가합니다.
|
||||
*
|
||||
* @param array $sources 기존 참조 소스 목록
|
||||
* @return array 게시판 소스가 추가된 목록
|
||||
*/
|
||||
public function addBoardSources(array $sources): array
|
||||
{
|
||||
$sources[] = ['table' => 'board_posts', 'columns' => ['content']];
|
||||
|
||||
return $sources;
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace Modules\Sirsoft\Board\Providers;
|
||||
use App\Extension\BaseModuleServiceProvider;
|
||||
use App\Seo\SitemapGenerator;
|
||||
use Modules\Sirsoft\Board\Console\Commands\AggregateBoardStatsCommand;
|
||||
use Modules\Sirsoft\Board\Console\Commands\PruneAttachmentsCommand;
|
||||
use Modules\Sirsoft\Board\Repositories\AttachmentRepository;
|
||||
use Modules\Sirsoft\Board\Repositories\BoardRepository;
|
||||
use Modules\Sirsoft\Board\Repositories\BoardStatRepository;
|
||||
@@ -88,6 +89,7 @@ class BoardServiceProvider extends BaseModuleServiceProvider
|
||||
*/
|
||||
protected array $commands = [
|
||||
AggregateBoardStatsCommand::class,
|
||||
PruneAttachmentsCommand::class,
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Modules\Sirsoft\Board\Repositories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\Sirsoft\Board\Enums\PostStatus;
|
||||
use Modules\Sirsoft\Board\Enums\TriggerType;
|
||||
@@ -431,4 +432,51 @@ class AttachmentRepository implements AttachmentRepositoryInterface
|
||||
{
|
||||
return Attachment::where('board_id', $boardId)->forceDelete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 게시글에 연결되지 않은 채 방치된 임시 첨부를 오래된 순으로 조회합니다.
|
||||
*
|
||||
* @param Carbon $threshold 기준 시각
|
||||
* @param int $limit 최대 조회 건수
|
||||
* @return Collection 임시 첨부 목록
|
||||
*/
|
||||
public function findStaleTempAttachments(Carbon $threshold, int $limit): Collection
|
||||
{
|
||||
return Attachment::query()
|
||||
->whereNotNull('temp_key')
|
||||
->whereNull('post_id')
|
||||
->where('created_at', '<', $threshold)
|
||||
->orderBy('created_at')
|
||||
->orderBy('id')
|
||||
->limit($limit)
|
||||
->get(['id', 'board_id', 'temp_key', 'disk', 'path', 'created_at']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 소프트 삭제된 지 오래된 첨부를 오래된 순으로 조회합니다.
|
||||
*
|
||||
* @param Carbon $threshold 기준 시각
|
||||
* @param int $limit 최대 조회 건수
|
||||
* @return Collection 소프트 삭제 첨부 목록
|
||||
*/
|
||||
public function findSoftDeletedOlderThan(Carbon $threshold, int $limit): Collection
|
||||
{
|
||||
return Attachment::onlyTrashed()
|
||||
->where('deleted_at', '<', $threshold)
|
||||
->orderBy('deleted_at')
|
||||
->orderBy('id')
|
||||
->limit($limit)
|
||||
->get(['id', 'board_id', 'post_id', 'disk', 'path', 'deleted_at']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 첨부 레코드를 영구 삭제합니다.
|
||||
*
|
||||
* @param Attachment $attachment 첨부파일 모델
|
||||
* @return bool 삭제 성공 여부
|
||||
*/
|
||||
public function forceDelete(Attachment $attachment): bool
|
||||
{
|
||||
return (bool) $attachment->forceDelete();
|
||||
}
|
||||
}
|
||||
+33
@@ -3,6 +3,7 @@
|
||||
namespace Modules\Sirsoft\Board\Repositories\Contracts;
|
||||
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Modules\Sirsoft\Board\Models\Attachment;
|
||||
use Modules\Sirsoft\Board\Models\Post;
|
||||
|
||||
@@ -198,4 +199,36 @@ interface AttachmentRepositoryInterface
|
||||
* @return int 삭제된 첨부파일 수
|
||||
*/
|
||||
public function forceDeleteByBoardId(int $boardId): int;
|
||||
|
||||
/**
|
||||
* 게시글에 연결되지 않은 채 방치된 임시 첨부를 오래된 순으로 조회합니다.
|
||||
*
|
||||
* 글쓰기 폼에서 업로드만 하고 저장 없이 이탈하면 `temp_key` 가 남은 채
|
||||
* `board_id = 0` 인 행과 그 파일이 영구 잔존합니다. 그 회수 대상을 찾습니다.
|
||||
*
|
||||
* @param Carbon $threshold 기준 시각 (이 시각 이전 업로드가 대상)
|
||||
* @param int $limit 최대 조회 건수
|
||||
* @return Collection 임시 첨부 목록 (created_at 오름차순)
|
||||
*/
|
||||
public function findStaleTempAttachments(Carbon $threshold, int $limit): Collection;
|
||||
|
||||
/**
|
||||
* 소프트 삭제된 지 오래된 첨부를 오래된 순으로 조회합니다.
|
||||
*
|
||||
* 첨부 삭제는 소프트 삭제만 수행하므로(휴지통 복원 대비) 물리 파일은 남아 있습니다.
|
||||
* 보존기간이 지난 뒤 파일과 기록을 함께 파기할 대상을 찾습니다.
|
||||
*
|
||||
* @param Carbon $threshold 기준 시각 (이 시각 이전 삭제분이 대상)
|
||||
* @param int $limit 최대 조회 건수
|
||||
* @return Collection 소프트 삭제 첨부 목록 (deleted_at 오름차순)
|
||||
*/
|
||||
public function findSoftDeletedOlderThan(Carbon $threshold, int $limit): Collection;
|
||||
|
||||
/**
|
||||
* 첨부 레코드를 영구 삭제합니다.
|
||||
*
|
||||
* @param Attachment $attachment 첨부파일 모델
|
||||
* @return bool 삭제 성공 여부
|
||||
*/
|
||||
public function forceDelete(Attachment $attachment): bool;
|
||||
}
|
||||
@@ -9,6 +9,7 @@ use App\Support\ImageResizer;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -440,8 +441,9 @@ class AttachmentService
|
||||
// Before 훅
|
||||
HookManager::doAction('sirsoft-board.attachment.before_delete', $attachment);
|
||||
|
||||
// 물리 파일은 삭제하지 않음 — 소프트 딜리트만 수행
|
||||
// 추후 배치 작업(Artisan Command + Scheduler)으로 보존 기간 경과 후 정리 예정
|
||||
// 물리 파일은 삭제하지 않음 — 소프트 딜리트만 수행 (휴지통 복원 대비).
|
||||
// 보존기간이 지난 뒤의 파일·기록 파기는 `sirsoft-board:prune-attachments` 가
|
||||
// 담당한다 (purgeSoftDeleted). 운영자가 설정에서 켰을 때만 동작한다.
|
||||
|
||||
// DB에서 소프트 삭제
|
||||
$result = $this->repository->delete($slug, $id);
|
||||
@@ -463,6 +465,151 @@ class AttachmentService
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 게시글에 연결되지 않은 채 방치된 임시 첨부를 정리합니다.
|
||||
*
|
||||
* 글쓰기 폼에서 파일만 올리고 저장하지 않고 이탈하면 `temp_key` 만 남은 행과 그 파일이
|
||||
* 남습니다. 연결 시점에 본경로로 옮겨지므로 `temp_key` 가 남아 있다는 것은 "끝내 연결되지
|
||||
* 않았다" 는 뜻이고, 그래서 오탐 여지가 없습니다.
|
||||
*
|
||||
* 파일 → 행 순서를 지켜, 행이 먼저 사라져 파일을 못 찾는 상태를 만들지 않습니다.
|
||||
*
|
||||
* @param int $days 보존기간(일)
|
||||
* @param int $limit 한 회차에 처리할 최대 건수
|
||||
* @param bool $dryRun true 면 대상만 세고 삭제하지 않음
|
||||
* @return array{scanned: int, deleted: int, failed: int} 처리 결과
|
||||
*/
|
||||
public function pruneTempUploads(int $days, int $limit, bool $dryRun = false): array
|
||||
{
|
||||
$threshold = Carbon::now()->subDays($days);
|
||||
$attachments = $this->repository->findStaleTempAttachments($threshold, $limit);
|
||||
|
||||
$result = $this->purgeCollection($attachments, $dryRun, '임시 게시판 첨부');
|
||||
|
||||
if (! $dryRun) {
|
||||
$this->removeEmptyTempDirectories($attachments);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 파일을 모두 지운 temp_key 디렉토리를 정리합니다.
|
||||
*
|
||||
* 파일만 지우고 디렉토리를 남기면 폼 세션마다 빈 디렉토리가 쌓여, 정리를 돌려도
|
||||
* 저장소에는 흔적이 계속 늘어납니다.
|
||||
*
|
||||
* 디렉토리에 파일이 남아 있으면(같은 temp_key 의 다른 첨부가 limit 에 걸려 이번 회차에서
|
||||
* 빠진 경우 등) 삭제하지 않습니다.
|
||||
*
|
||||
* @param Collection $attachments 이번 회차에 처리한 첨부 목록
|
||||
*/
|
||||
private function removeEmptyTempDirectories(Collection $attachments): void
|
||||
{
|
||||
$directories = [];
|
||||
|
||||
foreach ($attachments as $attachment) {
|
||||
$directory = dirname((string) $attachment->path);
|
||||
|
||||
if ($directory === '' || $directory === '.') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$directories[$attachment->disk.'|'.$directory] = [$attachment->disk, $directory];
|
||||
}
|
||||
|
||||
foreach ($directories as [$disk, $directory]) {
|
||||
$storage = $this->storageForRow($disk);
|
||||
|
||||
if ($storage->files('attachments', $directory) !== []) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$storage->deleteDirectory('attachments', $directory);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 소프트 삭제된 지 보존기간이 지난 첨부를 파일까지 영구 파기합니다.
|
||||
*
|
||||
* 첨부 삭제는 소프트 삭제만 수행하므로 파일이 남아 있습니다. 게시글 복원
|
||||
* (restoreCascadedByPostId)은 첨부의 `deleted_at` 이 보존기간 안에 있을 때에만 첨부까지
|
||||
* 온전히 복원되므로, 보존기간은 휴지통 복원 가능 기간과 같게 유지해야 합니다.
|
||||
*
|
||||
* @param int $days 보존기간(일)
|
||||
* @param int $limit 한 회차에 처리할 최대 건수
|
||||
* @param bool $dryRun true 면 대상만 세고 삭제하지 않음
|
||||
* @return array{scanned: int, deleted: int, failed: int} 처리 결과
|
||||
*/
|
||||
public function purgeSoftDeleted(int $days, int $limit, bool $dryRun = false): array
|
||||
{
|
||||
$threshold = Carbon::now()->subDays($days);
|
||||
$attachments = $this->repository->findSoftDeletedOlderThan($threshold, $limit);
|
||||
|
||||
return $this->purgeCollection($attachments, $dryRun, '소프트 삭제 게시판 첨부');
|
||||
}
|
||||
|
||||
/**
|
||||
* 첨부 컬렉션을 파일 → 기록 순으로 영구 파기합니다.
|
||||
*
|
||||
* @param Collection $attachments 대상 첨부 목록
|
||||
* @param bool $dryRun true 면 대상만 세고 삭제하지 않음
|
||||
* @param string $label 로그용 대상 설명
|
||||
* @return array{scanned: int, deleted: int, failed: int} 처리 결과
|
||||
*/
|
||||
private function purgeCollection(Collection $attachments, bool $dryRun, string $label): array
|
||||
{
|
||||
$result = ['scanned' => $attachments->count(), 'deleted' => 0, 'failed' => 0];
|
||||
|
||||
if ($dryRun) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
foreach ($attachments as $attachment) {
|
||||
$storage = $this->storageForRow($attachment->disk);
|
||||
|
||||
if ($storage->exists('attachments', $attachment->path) && ! $storage->delete('attachments', $attachment->path)) {
|
||||
Log::warning("{$label} 파일 삭제 실패 — 기록 보존", [
|
||||
'attachment_id' => $attachment->id,
|
||||
'disk' => $attachment->disk,
|
||||
'path' => $attachment->path,
|
||||
]);
|
||||
|
||||
$result['failed']++;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->repository->forceDelete($attachment);
|
||||
$result['deleted']++;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 첨부 행에 기록된 disk 기준 스토리지를 반환합니다.
|
||||
*
|
||||
* 디스크를 전환한 뒤에도 전환 이전 행의 파일을 그 행의 실제 저장 위치에서 지우기 위한
|
||||
* 해석입니다. 미등록 disk(그 디스크를 제공하던 확장이 비활성화된 경우)는 주입 스토리지로
|
||||
* 폴백합니다 — withDisk 로 미등록 disk 인스턴스를 만들면 이후 호출이 예외가 됩니다.
|
||||
*
|
||||
* @param string|null $disk 행의 disk 컬럼 값
|
||||
* @return StorageInterface 행 disk 의 스토리지
|
||||
*/
|
||||
private function storageForRow(?string $disk): StorageInterface
|
||||
{
|
||||
if ($disk === null || $disk === '' || $disk === $this->storage->getDisk()) {
|
||||
return $this->storage;
|
||||
}
|
||||
|
||||
if (config("filesystems.disks.{$disk}") === null) {
|
||||
return $this->storage;
|
||||
}
|
||||
|
||||
return $this->storage->withDisk($disk);
|
||||
}
|
||||
|
||||
/**
|
||||
* 순서 변경
|
||||
*
|
||||
|
||||
@@ -401,6 +401,10 @@ return [
|
||||
'override_values.max_file_size' => 'Max File Size',
|
||||
'override_values.max_file_count' => 'Max File Count',
|
||||
'override_values.new_display_hours' => 'New Display Hours',
|
||||
|
||||
// attachment_settings
|
||||
'attachment_settings.purge_enabled' => 'Permanent Cleanup Of Deleted Attachments',
|
||||
'attachment_settings.purge_retention_days' => 'Deleted Attachment Retention Period',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -401,6 +401,10 @@ return [
|
||||
'override_values.max_file_size' => '최대 파일 크기',
|
||||
'override_values.max_file_count' => '최대 파일 개수',
|
||||
'override_values.new_display_hours' => '신규 표시 시간',
|
||||
|
||||
// attachment_settings
|
||||
'attachment_settings.purge_enabled' => '삭제 첨부 영구 정리',
|
||||
'attachment_settings.purge_retention_days' => '삭제 첨부 보존기간',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
Reference in new issue
Block a user