Skip to content
This repository has been archived by the owner on Apr 16, 2021. It is now read-only.

Added "removeRootDir" option to directory uploads #32

Merged
merged 7 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const config = {
portalFileFieldname: "file",
portalDirectoryFileFieldname: "files[]",
customFilename: "",
removeRootDir: false,
},
DefaultDownloadOptions: {
portalUrl: "https://siasky.net",
Expand Down
5 changes: 4 additions & 1 deletion src/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const axios = require("axios");
const FormData = require("form-data");
const fs = require("fs");
const p = require("path");

const { walkDirectory, trimTrailingSlash } = require("./utils");

Expand Down Expand Up @@ -34,7 +35,9 @@ function UploadDirectory(path, opts) {

const formData = new FormData();
for (const file of walkDirectory(path)) {
formData.append(opts.portalDirectoryFileFieldname, fs.createReadStream(file), { filepath: file });
let filepath = file;
if (opts.removeRootDir) filepath = file.replace(p.normalize(path), "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you normalize the path before it gets passed to walkDirectory? I'm afraid that walkDirectory might otherwise return non-normalized paths, in which case this replace call wouldn't work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

walkDirectory uses path.join() which returns a normalized path so I think this is unnecessary?
See https://nodejs.org/api/path.html#path_path_join_paths

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, but I would still feel more comfortable normalizing before we pass path into a different function. Maybe it's okay now but we are making assumptions about walkDirectory which could change. I like sanitizing inputs as soon as possible in a function but I'll leave it up to you.

formData.append(opts.portalDirectoryFileFieldname, fs.createReadStream(file), { filepath });
}

const url = `${trimTrailingSlash(opts.portalUrl)}${trimTrailingSlash(opts.portalUploadPath)}?filename=${
Expand Down