Files
fastapi_file_uploader_for_c…/static/index.html

436 lines
13 KiB
HTML

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Uploader</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1000px;
margin: 0 auto;
}
h1 {
color: white;
text-align: center;
margin-bottom: 30px;
font-size: 2.5rem;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}
.card {
background: white;
border-radius: 16px;
padding: 30px;
margin-bottom: 20px;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}
.card h2 {
color: #333;
margin-bottom: 20px;
font-size: 1.5rem;
border-bottom: 2px solid #667eea;
padding-bottom: 10px;
}
.upload-area {
border: 3px dashed #667eea;
border-radius: 12px;
padding: 40px;
text-align: center;
cursor: pointer;
transition: all 0.3s ease;
background: #f8f9ff;
}
.upload-area:hover {
background: #eef0ff;
border-color: #764ba2;
}
.upload-area.dragover {
background: #e0e4ff;
border-color: #764ba2;
}
.upload-icon {
font-size: 48px;
margin-bottom: 10px;
}
.upload-text {
color: #666;
font-size: 1.1rem;
}
#fileInput {
display: none;
}
.btn {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 12px 24px;
border-radius: 8px;
font-size: 1rem;
cursor: pointer;
transition: transform 0.2s, box-shadow 0.2s;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
}
.btn:disabled {
background: #ccc;
cursor: not-allowed;
transform: none;
}
.btn-delete {
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a5a 100%);
padding: 6px 12px;
font-size: 0.85rem;
}
.btn-download {
background: linear-gradient(135deg, #4ecdc4 0%, #44a08d 100%);
padding: 6px 12px;
font-size: 0.85rem;
}
.progress-container {
margin-top: 20px;
display: none;
}
.progress-bar {
width: 100%;
height: 20px;
background: #e0e0e0;
border-radius: 10px;
overflow: hidden;
}
.progress-fill {
height: 100%;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
width: 0%;
transition: width 0.3s ease;
}
.progress-text {
text-align: center;
margin-top: 10px;
color: #666;
}
.file-list {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
.file-list th, .file-list td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #eee;
}
.file-list th {
background: #f8f9ff;
color: #333;
font-weight: 600;
}
.file-list tr:hover {
background: #f8f9ff;
}
.category-badge {
display: inline-block;
padding: 4px 12px;
border-radius: 20px;
font-size: 0.85rem;
font-weight: 500;
}
.category-image {
background: #e3f2fd;
color: #1976d2;
}
.category-video {
background: #fce4ec;
color: #c2185b;
}
.category-file {
background: #f3e5f5;
color: #7b1fa2;
}
.empty-state {
text-align: center;
padding: 40px;
color: #999;
}
.message {
padding: 12px 20px;
border-radius: 8px;
margin-top: 20px;
display: none;
}
.message.success {
background: #d4edda;
color: #155724;
display: block;
}
.message.error {
background: #f8d7da;
color: #721c24;
display: block;
}
.actions {
display: flex;
gap: 8px;
}
</style>
</head>
<body>
<div class="container">
<h1>File Uploader</h1>
<div class="card">
<h2>파일 업로드</h2>
<div class="upload-area" id="uploadArea">
<div class="upload-icon">📁</div>
<p class="upload-text">파일을 드래그하거나 클릭하여 업로드하세요</p>
<p class="upload-text" style="font-size: 0.9rem; color: #999; margin-top: 10px;">최대 100MB까지 업로드 가능</p>
</div>
<input type="file" id="fileInput" multiple>
<div class="progress-container" id="progressContainer">
<div class="progress-bar">
<div class="progress-fill" id="progressFill"></div>
</div>
<p class="progress-text" id="progressText">업로드 중...</p>
</div>
<div class="message" id="message"></div>
</div>
<div class="card">
<h2>파일 목록</h2>
<div id="fileListContainer">
<table class="file-list" id="fileList">
<thead>
<tr>
<th>원본 파일명</th>
<th>크기</th>
<th>카테고리</th>
<th>업로드 일시</th>
<th>작업</th>
</tr>
</thead>
<tbody id="fileTableBody">
</tbody>
</table>
<div class="empty-state" id="emptyState">
업로드된 파일이 없습니다.
</div>
</div>
</div>
</div>
<script>
const API_BASE = '';
const uploadArea = document.getElementById('uploadArea');
const fileInput = document.getElementById('fileInput');
const progressContainer = document.getElementById('progressContainer');
const progressFill = document.getElementById('progressFill');
const progressText = document.getElementById('progressText');
const message = document.getElementById('message');
const fileTableBody = document.getElementById('fileTableBody');
const emptyState = document.getElementById('emptyState');
// 업로드 영역 클릭
uploadArea.addEventListener('click', () => fileInput.click());
// 드래그 앤 드롭
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('dragover');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('dragover');
const files = e.dataTransfer.files;
handleFiles(files);
});
// 파일 선택
fileInput.addEventListener('change', (e) => {
handleFiles(e.target.files);
});
// 파일 처리
async function handleFiles(files) {
for (const file of files) {
await uploadFile(file);
}
loadFileList();
fileInput.value = '';
}
// 파일 업로드
async function uploadFile(file) {
const formData = new FormData();
formData.append('file', file);
progressContainer.style.display = 'block';
progressFill.style.width = '0%';
progressText.textContent = `${file.name} 업로드 중...`;
try {
const response = await fetch(`${API_BASE}/api/files/upload`, {
method: 'POST',
body: formData
});
const result = await response.json();
if (response.ok) {
progressFill.style.width = '100%';
progressText.textContent = '업로드 완료!';
showMessage(`${file.name} 업로드 성공!`, 'success');
} else {
throw new Error(result.detail || '업로드 실패');
}
} catch (error) {
showMessage(`업로드 실패: ${error.message}`, 'error');
} finally {
setTimeout(() => {
progressContainer.style.display = 'none';
}, 1000);
}
}
// 메시지 표시
function showMessage(text, type) {
message.textContent = text;
message.className = `message ${type}`;
setTimeout(() => {
message.className = 'message';
}, 3000);
}
// 파일 목록 로드
async function loadFileList() {
try {
const response = await fetch(`${API_BASE}/api/files/list`);
const data = await response.json();
fileTableBody.innerHTML = '';
if (data.files.length === 0) {
emptyState.style.display = 'block';
document.getElementById('fileList').style.display = 'none';
} else {
emptyState.style.display = 'none';
document.getElementById('fileList').style.display = 'table';
data.files.forEach(file => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${escapeHtml(file.original_filename)}</td>
<td>${formatSize(file.size)}</td>
<td><span class="category-badge category-${file.category}">${file.category}</span></td>
<td>${formatDate(file.upload_date)}</td>
<td class="actions">
<button class="btn btn-download" onclick="downloadFile('${file.saved_filename}')">다운로드</button>
<button class="btn btn-delete" onclick="deleteFile('${file.saved_filename}')">삭제</button>
</td>
`;
fileTableBody.appendChild(row);
});
}
} catch (error) {
console.error('파일 목록 로드 실패:', error);
}
}
// 파일 다운로드
function downloadFile(filename) {
window.location.href = `${API_BASE}/api/files/download/${filename}`;
}
// 파일 삭제
async function deleteFile(filename) {
if (!confirm('정말 삭제하시겠습니까?')) return;
try {
const response = await fetch(`${API_BASE}/api/files/${filename}`, {
method: 'DELETE'
});
if (response.ok) {
showMessage('파일이 삭제되었습니다.', 'success');
loadFileList();
} else {
const result = await response.json();
throw new Error(result.detail || '삭제 실패');
}
} catch (error) {
showMessage(`삭제 실패: ${error.message}`, 'error');
}
}
// 크기 포맷
function formatSize(bytes) {
if (bytes < 1024) return bytes + ' B';
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(2) + ' KB';
return (bytes / (1024 * 1024)).toFixed(2) + ' MB';
}
// 날짜 포맷
function formatDate(dateStr) {
const date = new Date(dateStr);
return date.toLocaleString('ko-KR');
}
// HTML 이스케이프
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// 초기 로드
loadFileList();
</script>
</body>
</html>