Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const axios = require('axios')
- const cheerio = require('cheerio')
- const FormData = require('form-data')
- class Utils {
- isUrl = url => {
- try {
- new URL(url)
- return true
- } catch {
- return false
- }
- }
- toBuffer = async i => {
- try {
- const file = Buffer.isBuffer(i) ? i : this.isUrl(i) ? await (await axios.get(i, {
- responseType: 'arraybuffer'
- })).data : null
- return Buffer.from(file)
- } catch (e) {
- return null
- }
- }
- randomString = length => {
- const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
- let result = ''
- for (let i = 0; i < length; i++) {
- const randomIndex = Math.floor(Math.random() * characters.length)
- result += characters[randomIndex]
- }
- return result
- }
- getExt = async buffer => {
- try {
- const { ext } = await require('file-type').fromBuffer(buffer)
- return ext ? ext : 'txt'
- } catch {
- return 'txt'
- }
- }
- }
- module.exports = class Ufile extends Utils {
- constructor() {
- super()
- this.headers = {
- 'Accept': 'application/json, text/plain, */*',
- 'Accept-Language': 'en-US,en;q=0.9,id;q=0.8',
- 'Cache-Control': 'no-cache',
- 'Connection': 'Keep-Alive',
- 'Origin': 'https://ufile.io',
- 'Pragma': 'no-cache',
- 'Referer': 'https://ufile.io/',
- 'Referrer-Policy': 'strict-origin-when-cross-origin',
- 'User-Agent': 'Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
- 'X-Requested-With': 'XMLHttpRequest',
- 'X-Forwarded-For': Array(4).fill(0).map(() => Math.floor(Math.random() * 256)).join('.')
- }
- }
- storage = input => new Promise(async resolve => {
- try {
- const file = await this.toBuffer(input)
- const parse = await axios.get('https://ufile.io/', {
- headers: this.headers
- })
- const cookie = parse.headers['set-cookie'].join(';')
- const token = cheerio.load(parse.data)('input[id="csrf_hash"]').attr('value')
- const session_id = cheerio.load(parse.data)('input[id="session_id"]').attr('value')
- let form = new FormData
- form.append('csrf_test_name', token)
- const json = await (await axios.post('https://ufile.io/v1/upload/select_storage', form, {
- headers: {
- ...this.headers,
- 'Cookie': cookie,
- ...form.getHeaders()
- }
- })).data
- let create = new FormData
- create.append('csrf_test_name', token)
- create.append('file_size', file.length)
- const session = await (await axios.post(json.storageBaseUrl + 'v1/upload/create_session', create, {
- headers: {
- ...this.headers,
- 'Cookie': cookie,
- ...create.getHeaders()
- }
- })).data
- resolve({
- creator: global.creator,
- status: true,
- data: {
- cookie,
- fuid: session.fuid,
- session_id,
- file,
- token,
- url: json.storageBaseUrl
- }
- })
- } catch (e) {
- resolve({
- creator: global.creator,
- status: false,
- msg: e.message
- })
- }
- })
- upload = input => new Promise(async resolve => {
- try {
- const parse = await this.storage(input)
- if (!parse.status) return resolve(parse)
- const fname = this.randomString(10)
- const fext = await this.getExt(parse.data.file)
- let form = new FormData
- form.append('chunk_index', 1)
- form.append('fuid', parse.data.fuid)
- form.append('file', parse.data.file, `${fname}.${fext}`)
- const result = await (await axios.post(parse.data.url + '/v1/upload/chunk', form, {
- headers: {
- ...this.headers,
- 'Cookie': parse.data.cookie,
- ...form.getHeaders()
- }
- })).data
- if (result != 'Uploaded successfully.') return resolve({
- creator: global.creator,
- status: false,
- msg: result
- })
- let fn = new FormData
- fn.append('csrf_test_name', parse.data.token)
- fn.append('fuid', parse.data.fuid)
- fn.append('file_name', fname + '.' + fext)
- fn.append('session_id', parse.data.session_id)
- fn.append('file_type', fext)
- fn.append('total_chunks', 1)
- fn.append('session_id', parse.data.session_id)
- const final = await (await axios.post(parse.data.url + '/v1/upload/finalise', fn, {
- headers: {
- ...this.headers,
- 'Cookie': parse.data.cookie,
- ...fn.getHeaders()
- }
- })).data
- if (!final.url) return resolve({
- creator: global.creator,
- status: false,
- msg: 'Failed to upload file'
- })
- resolve({
- creator: global.creator,
- status: true,
- data: final
- })
- } catch (e) {
- resolve({
- creator: global.creator,
- status: false,
- msg: e.message
- })
- }
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement