Skip to content

Commit

Permalink
allow uploading to cwd
Browse files Browse the repository at this point in the history
closes #79
  • Loading branch information
mifi committed Oct 22, 2024
1 parent 0583756 commit 9c14e6c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 6 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ export default ({ sharedPath: sharedPathIn, port, maxUploadSize, zipCompressionL
// NOTE: Must support non latin characters
app.post('/api/upload', bodyParser.json(), asyncHandler(async (req, res) => {
// console.log(req.headers)
const uploadDirPathIn = req.query.path || '/';

const uploadDirPath = await getFileAbsPath(uploadDirPathIn);

// parse a file upload
const form = Formidable({
keepExtensions: true,
uploadDir: sharedPath,
uploadDir: uploadDirPath,
maxFileSize: maxUploadSize,
maxFields,
});
Expand All @@ -67,12 +70,12 @@ export default ({ sharedPath: sharedPathIn, port, maxUploadSize, zipCompressionL
const files = Array.isArray(filesIn) ? filesIn : [filesIn];

// console.log(JSON.stringify({ fields, files }, null, 2));
console.log('Uploaded files:');
console.log('Uploaded files to', uploadDirPath);
files.forEach((f) => console.log(f.originalFilename, `(${f.size} bytes)`));

await pMap(files, async (file) => {
try {
const targetPath = join(sharedPath, filenamify(file.originalFilename, { maxLength: 255 }));
const targetPath = join(uploadDirPath, filenamify(file.originalFilename, { maxLength: 255 }));
if (!(await pathExists(targetPath))) await fs.rename(file.filepath, targetPath); // to prevent overwrites
} catch (err2) {
console.error(`Failed to rename ${file.originalFilename}`, err2);
Expand Down
8 changes: 4 additions & 4 deletions ezshare-frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const Section = ({ children, style }) => (
</div>
);

const Uploader = ({ onUploadSuccess }) => {
const Uploader = ({ onUploadSuccess, cwd }) => {
const [uploadProgress, setUploadProgress] = useState();
const [uploadSpeed, setUploadSpeed] = useState();

Expand Down Expand Up @@ -77,7 +77,7 @@ const Uploader = ({ onUploadSuccess }) => {
if (dataLoaded && startTime) setUploadSpeed(dataLoaded / ((Date.now() - startTime) / 1000));
};

await axios.post('/api/upload', data, { onUploadProgress });
await axios.post(`/api/upload?path=${encodeURIComponent(cwd)}`, data, { onUploadProgress });

Toast.fire({ icon: 'success', title: 'File(s) uploaded successfully' });
onUploadSuccess();
Expand All @@ -92,7 +92,7 @@ const Uploader = ({ onUploadSuccess }) => {
}

upload();
}, [onUploadSuccess]);
}, [cwd, onUploadSuccess]);

const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop });

Expand Down Expand Up @@ -288,7 +288,7 @@ const Browser = () => {

<Section>
<h2>Upload files</h2>
<Uploader onUploadSuccess={handleUploadSuccess} />
<Uploader cwd={currentDirFiles.cwd} onUploadSuccess={handleUploadSuccess} />
</Section>

<Section>
Expand Down

0 comments on commit 9c14e6c

Please sign in to comment.