initial
This commit is contained in:
commit
74ae93a96e
1 changed files with 94691 additions and 0 deletions
58
joplin notion extension/Web Development/API/sdrive.md
Normal file
58
joplin notion extension/Web Development/API/sdrive.md
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
sdrive(do not work in client side)
|
||||
|
||||
```jsx
|
||||
import axios from "axios";
|
||||
import FormData from "form-data";
|
||||
import fs from "fs";
|
||||
import dotenv from "dotenv";
|
||||
import mime from "mime-types";
|
||||
import path from "path";
|
||||
dotenv.config();
|
||||
async function uploadFile(fileStream, filename) {
|
||||
let formData = new FormData();
|
||||
let apikey = process.env.apikey;
|
||||
let base_url = process.env.base_url || "https://v3.sdrive.app";
|
||||
if (!apikey) {
|
||||
console.log("Please add your credentials to the .env file");
|
||||
process.exit();
|
||||
}
|
||||
|
||||
let mimetype = mime.lookup(path.extname(filename));
|
||||
formData.append("fileupload", fileStream, {
|
||||
filename: filename,
|
||||
contentType: mimetype,
|
||||
});
|
||||
formData.append("apikey", apikey);
|
||||
formData.append("network", "shdwdrive");
|
||||
|
||||
return await axios
|
||||
.post(base_url + "/upload", formData, {
|
||||
headers: formData.getHeaders(), // Set headers from formData
|
||||
})
|
||||
.catch((error) => {
|
||||
const errorInfo = {
|
||||
status: error.response.status,
|
||||
statusText: error.response.statusText,
|
||||
data: error.response.data,
|
||||
};
|
||||
throw Error(JSON.stringify(errorInfo));
|
||||
})
|
||||
.then((response) => {
|
||||
return response.data;
|
||||
});
|
||||
}
|
||||
|
||||
// Example usage
|
||||
(async () => {
|
||||
const inputPath = process.argv[2];
|
||||
const filename = inputPath.split("/").pop();
|
||||
const fileStream = fs.createReadStream(inputPath);
|
||||
try {
|
||||
const response = await uploadFile(fileStream, inputPath);
|
||||
console.log(response);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
})();
|
||||
```
|
||||
|
||||
Loading…
Reference in a new issue