tus Protocol
tus යනු resumable file uploads සඳහා open protocol එකකි. Network බාධා වලින් පසුවද uploads දිගටම කරගෙන යාමට මෙය භාවිතා කරන්න.
tus භාවිතා කරන්නේ ඇයි?
- Resumable: Network failures වලින් පසු uploads දිගටම කරන්න
- Efficient: මුල සිටම නොව, නැවතුණු තැනින් resume කරන්න
- Standardized: බොහෝ server implementations සහිත හොඳින් අර්ථ දක්වා ඇති protocol
- Chunked: විශාල ගොනු ස්වයංක්රීයව හසුරුවයි
ස්ථාපනය
npm install @samithahansaka/dropup tus-js-client
tus-js-client යනු peer dependency එකකි.
මූලික භාවිතය
import { useDropup } from '@samithahansaka/dropup';
import { createTusUploader } from '@samithahansaka/dropup/tus';
function TusUploader() {
const { files, actions, state, getDropProps, getInputProps } = useDropup({
upload: createTusUploader({
endpoint: 'https://tusd.tusdemo.net/files/',
}),
onUploadComplete: (file) => {
console.log('Upload සම්පූර්ණයි:', file.uploadedUrl);
},
});
return (
<div>
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>Resumable upload සඳහා ගොනු දමන්න</p>
</div>
{files.map(file => (
<div key={file.id} style={styles.fileItem}>
<span>{file.name}</span>
<span>{file.progress}%</span>
<span>{file.status}</span>
</div>
))}
<button onClick={() => actions.upload()}>
Upload
</button>
</div>
);
}
වින්යාස විකල්ප
createTusUploader({
// අවශ්යයි
endpoint: 'https://your-tus-server.com/files/',
// Chunk size (පෙරනිමි: Infinity = chunking නැත)
chunkSize: 5 * 1024 * 1024, // 5MB chunks
// Retry configuration
retryDelays: [0, 1000, 3000, 5000], // 0s, 1s, 3s, 5s පසුව retry
// සියලුම requests සඳහා Headers
headers: {
'Authorization': 'Bearer token',
},
// Server වෙත යවන Metadata
metadata: {
filename: 'file.name',
filetype: 'file.type',
// Custom metadata එකතු කරන්න
userId: 'user123',
},
// localStorage හරහා resumability සක්රීය කරන්න
storeFingerprintForResuming: true,
// සාර්ථක upload පසු fingerprint ඉවත් කරන්න
removeFingerprintOnSuccess: true,
// Parallel uploads
parallelUploads: 3,
});
පෙර Uploads Resume කරන්න
tus වලට බාධිත uploads resume කළ හැක:
const { files, actions } = useDropup({
upload: createTusUploader({
endpoint: 'https://your-tus-server.com/files/',
// Resuming සඳහා upload URLs ගබඩා කරන්න
storeFingerprintForResuming: true,
// විකල්ප: custom storage key
urlStorageKey: 'my-app-tus-uploads',
onShouldRetry: (error, retryAttempt) => {
// Custom retry logic
if (error.message.includes('network')) {
return retryAttempt < 5;
}
return false;
},
}),
});
// නැවත එකතු කළ විට ගොනු ස්වයංක්රීයව resume විය හැක
// නැතහොත් retry සමඟ manually:
actions.retry(['file-id']);
Progress Tracking
function TusWithProgress() {
const { files, actions, getDropProps, getInputProps } = useDropup({
upload: createTusUploader({
endpoint: 'https://your-tus-server.com/files/',
}),
onUploadProgress: (file, progress) => {
console.log(`${file.name}: ${progress}%`);
},
onUploadStart: (file) => {
console.log(`Upload ආරම්භයි: ${file.name}`);
},
onUploadComplete: (file) => {
console.log(`සම්පූර්ණයි: ${file.uploadedUrl}`);
},
});
return (
<div>
<div {...getDropProps()}>
<input {...getInputProps()} />
<p>ගොනු මෙතැනට දමන්න</p>
</div>
{files.map(file => (
<div key={file.id}>
<span>{file.name}</span>
{file.status === 'uploading' && (
<div style={styles.progressBar}>
<div
style={{
...styles.progress,
width: `${file.progress}%`,
}}
/>
</div>
)}
<span>{file.status}</span>
</div>
))}
</div>
);
}
Pause සහ Resume
function PausableUploader() {
const { files, actions, getDropProps, getInputProps } = useDropup({
upload: createTusUploader({
endpoint: 'https://your-tus-server.com/files/',
storeFingerprintForResuming: true,
}),
});
return (
<div>
<div {...getDropProps()}>
<input {...getInputProps()} />
<p>ගොනු මෙතැනට දමන්න</p>
</div>
{files.map(file => (
<div key={file.id}>
<span>{file.name}</span>
<span>{file.progress}%</span>
{file.status === 'uploading' && (
<button onClick={() => actions.cancel(file.id)}>
විරාම
</button>
)}
{file.status === 'paused' && (
<button onClick={() => actions.retry([file.id])}>
Resume
</button>
)}
{file.status === 'error' && (
<button onClick={() => actions.retry([file.id])}>
නැවත උත්සාහ
</button>
)}
</div>
))}
<button onClick={() => actions.upload()}>
සියල්ල ආරම්භ කරන්න
</button>
</div>
);
}
Server Setup
tusd (Official Server)
# tusd install කරන්න
go install github.com/tus/tusd/cmd/tusd@latest
# පෙරනිමි options සමඟ run කරන්න
tusd -upload-dir=/uploads
# S3 storage සමඟ run කරන්න
tusd -s3-bucket=my-bucket -s3-endpoint=https://s3.amazonaws.com
Node.js (tus-node-server)
const { Server } = require('@tus/server');
const { FileStore } = require('@tus/file-store');
const server = new Server({
path: '/files',
datastore: new FileStore({ directory: './uploads' }),
});
// Express සමඟ
app.all('/files/*', server.handle.bind(server));
Custom Metadata Handling
// Server-side: metadata access කරන්න
const server = new Server({
path: '/files',
datastore: new FileStore({ directory: './uploads' }),
onUploadCreate: async (req, res, upload) => {
// Client එකෙන් යැවූ metadata access කරන්න
console.log(upload.metadata);
// { filename: 'photo.jpg', filetype: 'image/jpeg', userId: 'user123' }
},
onUploadFinish: async (req, res, upload) => {
// සම්පූර්ණ upload process කරන්න
console.log('Upload සම්පූර්ණයි:', upload.id);
},
});
tus Extensions
Dropup හි tus integration මෙම tus extensions සහය දක්වයි:
| Extension | විස්තරය |
|---|---|
| Creation | නව uploads සාදන්න |
| Termination | Uploads අවලංගු කරන්න |
| Checksum | Integrity සත්යාපනය |
| Concatenation | Parallel uploads |
| Expiration | Auto-cleanup |
Error Handling
const { files, actions } = useDropup({
upload: createTusUploader({
endpoint: 'https://your-tus-server.com/files/',
retryDelays: [0, 1000, 3000, 5000, 10000],
}),
onUploadError: (file, error) => {
if (error.code === 'TUS_OFFLINE') {
console.log('Network offline, සම්බන්ධ වූ විට resume වේ');
} else if (error.code === 'TUS_EXPIRED') {
console.log('Upload expired, නැවත ආරම්භ කරමින්');
actions.retry([file.id]);
} else {
console.error('Upload අසාර්ථකයි:', error.message);
}
},
});
Large File Uploads
tus විශාල ගොනු සඳහා සුදුසුයි:
function LargeFileUploader() {
const { files, actions, getDropProps, getInputProps } = useDropup({
maxSize: 10 * 1024 * 1024 * 1024, // 10GB සීමාව
upload: createTusUploader({
endpoint: 'https://your-tus-server.com/files/',
chunkSize: 50 * 1024 * 1024, // 50MB chunks
retryDelays: [0, 3000, 5000, 10000, 20000],
storeFingerprintForResuming: true,
}),
onUploadProgress: (file, progress) => {
// විශාල ගොනු සඳහා සවිස්තර progress පෙන්වන්න
const uploadedMB = (file.size * progress / 100 / 1024 / 1024).toFixed(0);
const totalMB = (file.size / 1024 / 1024).toFixed(0);
console.log(`${uploadedMB}MB / ${totalMB}MB`);
},
});
return (
<div {...getDropProps()}>
<input {...getInputProps()} />
<p>විශාල ගොනු upload කරන්න (10GB දක්වා)</p>
</div>
);
}
සංසන්දනය: tus vs Chunked Upload
| Feature | tus | Custom Chunked |
|---|---|---|
| Resumability | Built-in | Manual |
| Server support | බොහෝ options | Custom |
| Protocol overhead | ඉහළ | පහළ |
| Configuration | Standardized | Flexible |
| වඩාත් සුදුසු | විශාල ගොනු, අස්ථිර networks | සරල uploads |