基于 SOC 职业分类
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/puk0806/gugbab-claude --skill multipart-upload命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
DDD(Domain-Driven Design) 아키텍처 핵심 패턴 - 유비쿼터스 언어, 서브도메인, 바운디드 컨텍스트, Aggregate, Entity/VO, 도메인 서비스/이벤트, 레이어드 아키텍처
대규모 React/Next.js 프로젝트를 layer-first(types/·utils/·hooks/·api/·components/ 밑에 도메인이 반복되는 구조)에서 domain-first(feature/도메인 우선) 구조로 전환하는 설계 기준과 절차. Feature-Sliced Design 2.1 정본(layers 6종·slices·segments·import 규칙·@x 크로스임포트·public API), FSD를 쓰지 않는 경량 대안(features + shared 2~3계층 + ESLint import/no-restricted-paths), Next.js App Router 공존 전략(route group `()`·private folder `_`·colocation), Turborepo/Nx 모노레포에서 폴더↔패키지 승격 기준, colocation과 배럴 파일 성능 트레이드오프, 도메인 경계 역추출(import 그래프·change coupling·용어 클러스터), 전환 실패 패턴(shared 비대화·entities 남용·순환 의존·도메인=라우트 착각·조기 추상화). 도메인 개념 자체(바운디드 컨텍스트·유비쿼터스 언어)는 `architecture/ddd` 스킬을 참조한다.
소스 파일 수천 개 규모 프론트엔드 코드베이스를 멈추지 않고 점진 재구조화하는 실행 전략 - Strangler Fig / Branch by Abstraction / Parallel Change, ts-morph·jscodeshift codemod, PR 분할·검증 게이트·되돌리기, 테스트 없는 코드의 안전망, 작업 순서 설계와 위반 수 기반 진행 추적
| name | multipart-upload |
| description | Axum Multipart 파일 업로드 처리 — 필드 구분, 바이트 읽기, 파일 타입별 처리, 크기 제한, 에러 처리 |
소스: https://docs.rs/axum/latest/axum/extract/struct.Multipart.html 소스: https://docs.rs/axum/latest/axum/extract/struct.DefaultBodyLimit.html 검증일: 2026-06-20
주의: axum 0.8.x 기준입니다.
Multipart는 기본 feature가 아니므로Cargo.toml에 반드시 명시해야 합니다.
axum = { version = "0.8", features = ["multipart"] }
axum::extract::Multipart는 multipart/form-data 요청 바디를 파싱하는 extractor이다.
use axum::{
extract::Multipart,
routing::post,
Router,
};
async fn upload(mut multipart: Multipart) {
while let Some(field) = multipart.next_field().await.unwrap() {
let name = field.name().unwrap_or("unknown").to_string();
let data = field.bytes().await.unwrap();
println!("Field `{}`: {} bytes", name, data.len());
}
}
let app = Router::new().route("/upload", post(upload));
핵심 규칙:
Multipart는 mut으로 받아야 한다 (내부 상태가 변경됨)next_field()는 Option<Field>를 반환 — None이면 모든 필드 소진Field는 한 번만 소비 가능 (bytes/text/chunk 중 하나만 호출)Field 구조체의 메서드로 필드 종류를 판별한다.
| 메서드 | 반환 타입 | 설명 |
|---|---|---|
name() | Option<&str> | 폼 필드 이름 (<input name="...">) |
file_name() | Option<&str> | 파일명 (파일 필드에만 존재) |
content_type() | Option<&str> | MIME 타입 (파일 필드에만 존재) |
text() | Result<String, Error> | 필드를 문자열로 소비 |
bytes() | Result<Bytes, Error> | 필드를 바이트로 소비 |
chunk() | Result<Option<Bytes>, Error> | 스트리밍 방식으로 청크 단위 읽기 |
async fn upload(mut multipart: Multipart) {
while let Some(field) = multipart.next_field().await.unwrap() {
let name = field.name().unwrap_or("unknown").to_string();
if let Some(file_name) = field.file_name() {
// 파일 필드
let file_name = file_name.to_string();
let content_type = field.content_type()
.unwrap_or("application/octet-stream")
.to_string();
let data = field.bytes().await.unwrap();
println!("File: {} ({}, {} bytes)", file_name, content_type, data.len());
} else {
// 텍스트 필드
let value = field.text().await.unwrap();
println!(, name, value);
}
}
}
판별 기준: file_name()이 Some이면 파일, None이면 텍스트 필드이다.
let data: Bytes = field.bytes().await?;
메모리를 절약하려면 chunk()로 청크 단위 처리한다.
use tokio::io::AsyncWriteExt;
use tokio::fs::File;
async fn stream_to_file(field: &mut axum::extract::multipart::Field<'_>, path: &str) -> Result<u64, std::io::Error> {
let mut file = File::create(path).await?;
let mut total: u64 = 0;
while let Some(chunk) = field.chunk().await.map_err(|e| {
std::io::Error::new(std::io::ErrorKind::Other, e)
})? {
file.write_all(&chunk).await?;
total += chunk.len() as u64;
}
file.flush().await?;
Ok(total)
}
주의:
chunk()기반 스트리밍은Field의&mut참조가 필요합니다. 소유권 이동 후에는 사용할 수 없습니다.
MIME 타입 또는 확장자로 파일 종류를 구분한다.
| 파일 타입 | Content-Type | 확장자 |
|---|---|---|
application/pdf | .pdf | |
| Plain Text | text/plain | .txt |
| Word (docx) | application/vnd.openxmlformats-officedocument.wordprocessingml.document | .docx |
| Word (doc) | application/msword | .doc |
use std::path::Path;
async fn handle_upload(mut multipart: Multipart) -> Result<String, (StatusCode, String)> {
while let Some(field) = multipart.next_field().await
.map_err(|e| (StatusCode::BAD_REQUEST, e.to_string()))?
{
let Some(file_name) = field.file_name().map(|s| s.to_string()) else {
continue; // 텍스트 필드는 건너뜀
};
let content_type = field.content_type()
.unwrap_or("application/octet-stream")
.to_string();
// MIME 타입 기반 허용 목록
let allowed = [
"application/pdf",
"text/plain",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/msword",
];
if !allowed.contains(&content_type.as_str()) {
return Err((
StatusCode::UNSUPPORTED_MEDIA_TYPE,
format!(, content_type),
));
}
= Path::(&file_name)
.()
.(|e| e.())
.();
= [, , , ];
!allowed_ext.(&ext) {
((
StatusCode::UNSUPPORTED_MEDIA_TYPE,
(, ext),
));
}
= field.().
.(|e| (StatusCode::BAD_REQUEST, e.()))?;
= (, file_name);
tokio::fs::(&save_path, &data).
.(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.()))?;
}
(.())
}
주의: Content-Type은 클라이언트가 임의로 설정할 수 있으므로, 확장자 검증과 매직 바이트 검증을 병행하는 것을 권장합니다.
Axum은 기본적으로 요청 바디 크기를 2MB로 제한한다.
use axum::extract::DefaultBodyLimit;
// 라우터 전체에 적용
let app = Router::new()
.route("/upload", post(upload))
.layer(DefaultBodyLimit::max(10 * 1024 * 1024)); // 10MB
// 특정 라우트에만 적용
let app = Router::new()
.route("/upload", post(upload))
.route_layer(DefaultBodyLimit::max(50 * 1024 * 1024)); // 50MB
// 제한 해제 (권장하지 않음)
let app = Router::new()
.route("/upload", post(upload))
.layer(DefaultBodyLimit::disable());
use tower_http::limit::RequestBodyLimitLayer;
let app = Router::new()
.route("/upload", post(upload))
.layer(RequestBodyLimitLayer::new(10 * 1024 * 1024));
DefaultBodyLimit vs RequestBodyLimitLayer:
DefaultBodyLimit는 Axum 내장으로 Multipart, Json, Bytes 등 모든 extractor에 적용RequestBodyLimitLayer는 Tower 미들웨어로 http_body_util::Limited를 사용DefaultBodyLimit가 우선async fn upload(mut multipart: Multipart) -> Result<String, (StatusCode, String)> {
const MAX_FILE_SIZE: usize = 10 * 1024 * 1024; // 10MB
while let Some(field) = multipart.next_field().await
.map_err(|e| (StatusCode::BAD_REQUEST, e.to_string()))?
{
let data = field.bytes().await
.map_err(|e| (StatusCode::BAD_REQUEST, e.to_string()))?;
if data.len() > MAX_FILE_SIZE {
return Err((
StatusCode::PAYLOAD_TOO_LARGE,
"File exceeds 10MB limit".to_string(),
));
}
}
Ok("OK".to_string())
}
주의:
MultipartError는multer::Error를 래핑한 단일 구조체로 공개 variant가 없다. 에러는.to_string()으로 메시지 처리하거나, extractor 레벨은MultipartRejection으로 구분한다.
| 상황 | 처리 방법 |
|---|---|
| Content-Type이 multipart가 아님 | MultipartRejection::InvalidBoundary (extractor 레벨) |
| 바디 크기 초과 | 연결 종료 또는 .to_string()으로 확인 |
| 필드 읽기 실패 | .map_err(|e| e.to_string()) |
| 불완전한 multipart 데이터 | .to_string()으로 에러 메시지 처리 |
use axum::{
extract::Multipart,
http::StatusCode,
response::IntoResponse,
Json,
};
use serde_json::json;
enum UploadError {
MultipartError(String),
FileTooLarge { max: usize, actual: usize },
UnsupportedType(String),
IoError(std::io::Error),
}
impl IntoResponse for UploadError {
fn into_response(self) -> axum::response::Response {
let (status, message) = match self {
Self::MultipartError(msg) => (StatusCode::BAD_REQUEST, msg),
Self::FileTooLarge { max, actual } => (
StatusCode::PAYLOAD_TOO_LARGE,
format!("File size {}B exceeds limit {}B", actual, max),
),
Self::UnsupportedType(t) => (
StatusCode::UNSUPPORTED_MEDIA_TYPE,
format!("Unsupported: {}", t),
),
Self::IoError(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("IO error: {}", e),
),
};
(status, Json(json!({ "error": message }))).into_response()
}
}
async fn upload( multipart: Multipart) <Json<serde_json::Value>, UploadError> {
: <> = [];
(field) = multipart.().
.(|e| UploadError::(e.()))?
{
= field.() {
(name) => name.(),
=> ,
};
= field.().
.(|e| UploadError::(e.()))?;
data.() > * * {
(UploadError::FileTooLarge {
max: * * ,
actual: data.(),
});
}
tokio::fs::((, file_name), &data)
.
.(UploadError::IoError)?;
files.(file_name);
}
((json!({ : files })))
}
use axum::{
extract::{DefaultBodyLimit, Multipart},
http::StatusCode,
response::Json,
routing::post,
Router,
};
use serde_json::{json, Value};
use std::path::Path;
use tokio::net::TcpListener;
const MAX_BODY_SIZE: usize = 50 * 1024 * 1024; // 50MB
const UPLOAD_DIR: &str = "./uploads";
#[tokio::main]
async fn main() {
tokio::fs::create_dir_all(UPLOAD_DIR).await.unwrap();
let app = Router::new()
.route("/upload", post(handle_upload))
.layer(DefaultBodyLimit::max(MAX_BODY_SIZE));
let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn handle_upload(
mut multipart: Multipart,
) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
let allowed_ext = ["pdf", , , ];
= [];
(field) = multipart.().
.(|e| (StatusCode::BAD_REQUEST, (json!({: e.()}))))?
{
= field.() {
(name) => name.(),
=> ,
};
= Path::(&file_name)
.()
.(|e| e.())
.();
!allowed_ext.(&ext) {
((
StatusCode::UNSUPPORTED_MEDIA_TYPE,
(json!({: (, ext)})),
));
}
= field.().
.(|e| (StatusCode::BAD_REQUEST, (json!({: e.()}))))?;
= (, UPLOAD_DIR, file_name);
tokio::fs::(&path, &data).
.(|e| (StatusCode::INTERNAL_SERVER_ERROR, (json!({: e.()}))))?;
results.(json!({
: file_name,
: data.(),
}));
}
((json!({ : results })))
}
| 항목 | 값 |
|---|---|
| Extractor | axum::extract::Multipart |
| 기본 바디 크기 제한 | 2MB |
| 크기 제한 변경 | DefaultBodyLimit::max(bytes) |
| 파일 판별 | field.file_name().is_some() |
| 한 번에 읽기 | field.bytes().await |
| 스트리밍 읽기 | field.chunk().await (반복) |
| 필수 의존성 | axum, tokio (full feature) |