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 all commits
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
10 changes: 9 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, uriSkynetPrefix, trimTrailingSlash } = require("./utils");

Expand Down Expand Up @@ -46,8 +47,15 @@ function uploadDirectory(path, customOptions = {}) {
}

const formData = new FormData();
path = p.normalize(path);
var basepath = path;
if (basepath != "/") {
basepath += "/";
}
for (const file of walkDirectory(path)) {
formData.append(opts.portalDirectoryFileFieldname, fs.createReadStream(file), { filepath: file });
formData.append(opts.portalDirectoryFileFieldname, fs.createReadStream(file), {
filepath: file.replace(basepath, ""),
});
}

// Form the URL.
Expand Down
2 changes: 1 addition & 1 deletion src/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("uploadFile", () => {

describe("uploadDirectory", () => {
const filename = "testdata";
const directory = ["testdata/file1.txt", "testdata/file2.txt", "testdata/dir1/file3.txt"];
const directory = ["file1.txt", "file2.txt", "dir1/file3.txt"];

beforeEach(() => {
axios.post.mockResolvedValue({ data: { skylink } });
Expand Down