├── .env.example ├── .gitignore ├── LICENSE ├── README.md ├── main.js └── package.json /.env.example: -------------------------------------------------------------------------------- 1 | DIScORD_WEBHOOK_URL= -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | node_modules 3 | yarn.lock -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Vu Nguyen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # discord-storage 2 | 3 | Free unlimited file hosting using Discord 4 | 5 | ## Installation 6 | 7 | ```bash 8 | git clone https://github.com/hoangvu12/discord-storage 9 | cd discord-storage 10 | yarn install 11 | ``` 12 | 13 | ## Start the server 14 | 15 | 1. Create `.env` file, paste in your discord webhook url (look `.env.example` for example). 16 | 2. Run `yarn dev` for development or `yarn start` to start the server (or use npm) 17 | 3. Your server is up running on port 3000 18 | 19 | ## Result 20 | 21 | ```json 22 | { 23 | "success": true, 24 | "attachments": [ 25 | { 26 | "id": "989360005578883143", 27 | "filename": "2308-cute-anime-girl.png", 28 | "size": 7938, 29 | "url": "https://cdn.discordapp.com/attachments/989344480966627378/989360005578883143/2308-cute-anime-girl.png", 30 | "proxy_url": "https://media.discordapp.net/attachments/989344480966627378/989360005578883143/2308-cute-anime-girl.png", 31 | "width": 256, 32 | "height": 256, 33 | "content_type": "image/jpeg" 34 | }, 35 | { 36 | "id": "989360005838950410", 37 | "filename": "peakpx_2.jpg", 38 | "size": 12553, 39 | "url": "https://cdn.discordapp.com/attachments/989344480966627378/989360005838950410/peakpx_2.jpg", 40 | "proxy_url": "https://media.discordapp.net/attachments/989344480966627378/989360005838950410/peakpx_2.jpg", 41 | "width": 1000, 42 | "height": 563, 43 | "content_type": "image/jpeg" 44 | } 45 | ] 46 | } 47 | ``` 48 | 49 | ## Usage 50 | 51 | ### Upload one file 52 | 53 | ```js 54 | const photo = document.getElementById("image-file").files[0]; 55 | 56 | const formData = new FormData(); 57 | 58 | formData.append("file", photo); 59 | 60 | fetch("/upload", { method: "POST", body: formData }); 61 | ``` 62 | 63 | ### Upload files 64 | 65 | ```js 66 | const images = document.getElementById("image-file").files; 67 | 68 | const formData = new FormData(); 69 | 70 | images.forEach((image) => { 71 | formData.append("file", image); 72 | }); 73 | 74 | fetch("/upload", { method: "POST", body: formData }); 75 | ``` 76 | 77 | ### Upload with an url 78 | 79 | ```js 80 | const formData = new FormData(); 81 | 82 | formData.append("url", "https://example.com/image.jpg"); 83 | 84 | fetch("/upload", { method: "POST", body: formData }); 85 | ``` 86 | 87 | ### Upload with urls 88 | 89 | ```js 90 | const urls = [ 91 | "https://example.com/image.jpg", 92 | "https://example.com/image.png", 93 | "https://example.com/rickroll.png", 94 | ]; 95 | 96 | const formData = new FormData(); 97 | 98 | urls.forEach((url) => { 99 | formData.append("url", url); 100 | }); 101 | 102 | fetch("/upload", { method: "POST", body: formData }); 103 | ``` 104 | -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const fileUpload = require("express-fileupload"); 3 | const FormData = require("form-data"); 4 | const axios = require("axios"); 5 | const cors = require("cors"); 6 | const app = express(); 7 | const port = 3000; 8 | require("dotenv").config(); 9 | 10 | app.use( 11 | fileUpload({ 12 | limits: { fileSize: 8 * 1024 * 1024 }, 13 | }) 14 | ); 15 | 16 | app.use(cors()); 17 | 18 | app.use(express.urlencoded({ extended: true })); 19 | 20 | const uploadToDiscord = async (file) => { 21 | const discordWebhookUrl = process.env.DISCORD_WEBHOOK_URL; 22 | 23 | if (!discordWebhookUrl) 24 | throw new Error( 25 | 'Please specify your discord webhook in .env file by "DISCORD_WEBHOOK_URL" key' 26 | ); 27 | 28 | const formData = new FormData(); 29 | 30 | if (Array.isArray(file)) { 31 | file.forEach((file, index) => { 32 | formData.append(`files[${index}]`, file.data, file.name); 33 | }); 34 | } else { 35 | formData.append("file", file.data, file.name); 36 | } 37 | 38 | const { data } = await axios.post(discordWebhookUrl, formData, { 39 | headers: { "Content-Type": "multipart/form-data" }, 40 | }); 41 | 42 | if (!data?.attachments?.length) throw new Error("No attachments found"); 43 | 44 | return data?.attachments; 45 | }; 46 | 47 | const urlToFile = async (url) => { 48 | const filename = url 49 | .substring(url.lastIndexOf("/") + 1) 50 | .replace(/((\?|#).*)?$/, ""); 51 | 52 | const response = await axios.get(url, { responseType: "arraybuffer" }); 53 | const buffer = Buffer.from(response.data, "utf-8"); 54 | 55 | return { 56 | name: filename, 57 | data: buffer, 58 | }; 59 | }; 60 | 61 | const urlsToFiles = async (url) => { 62 | if (Array.isArray(url)) { 63 | const promises = url.map((url) => urlToFile(url)); 64 | 65 | return Promise.all(promises); 66 | } 67 | 68 | return urlToFile(url); 69 | }; 70 | 71 | app.get("/", (req, res) => { 72 | res.send("Hello World!"); 73 | }); 74 | 75 | app.post("/upload", async (req, res) => { 76 | try { 77 | if (!req.files?.file && !req.body?.url) { 78 | res.statusCode = 400; 79 | 80 | res.json({ 81 | success: false, 82 | error: "Please specify files or urls", 83 | }); 84 | 85 | return; 86 | } 87 | 88 | let attachments = []; 89 | 90 | if (req.files?.file) { 91 | attachments = await uploadToDiscord(req.files.file); 92 | } else { 93 | const files = await urlsToFiles(req.body.url); 94 | 95 | attachments = await uploadToDiscord(files); 96 | } 97 | 98 | res.json({ 99 | success: true, 100 | attachments, 101 | }); 102 | } catch (err) { 103 | console.log(err); 104 | 105 | res.statusCode = 500; 106 | res.json({ 107 | success: false, 108 | error: err.message, 109 | }); 110 | } 111 | }); 112 | 113 | app.listen(port, () => { 114 | console.log(`Example app listening on port ${port}`); 115 | }); 116 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "discord-storage", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node main.js", 9 | "dev": "nodemon main.js" 10 | }, 11 | "keywords": [], 12 | "author": "hoangvu12", 13 | "license": "ISC", 14 | "dependencies": { 15 | "axios": "^0.27.2", 16 | "cors": "^2.8.5", 17 | "dotenv": "^16.0.1", 18 | "express": "^4.18.1", 19 | "express-fileupload": "^1.4.0", 20 | "form-data": "^4.0.0" 21 | }, 22 | "devDependencies": { 23 | "nodemon": "^2.0.16" 24 | } 25 | } 26 | --------------------------------------------------------------------------------