Skip to content

Commit

Permalink
refactor: generate random string
Browse files Browse the repository at this point in the history
  • Loading branch information
arugaz committed Feb 10, 2023
1 parent e585f84 commit 3259843
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/utils/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import FormData from "form-data"
import axios, { AxiosRequestConfig } from "axios"
import { generateRandString } from "../utils/filesystem"

const fetchDefaultOptions: AxiosRequestConfig = {
headers: {
Expand Down Expand Up @@ -58,7 +59,7 @@ export const fetcherBuffer = (url: string, opts?: AxiosRequestConfig) =>
export const uploadMedia = (buffData: Buffer, ext: string) =>
new Promise<string>((resolve, reject) => {
const form = new FormData()
form.append("file", buffData, `temp${Date.now().toString(36)}.${ext}`)
form.append("file", buffData, `temp${generateRandString()}.${ext}`)
fetcherPOST("https://telegra.ph/upload", {
data: form,
headers: {
Expand Down
16 changes: 15 additions & 1 deletion src/utils/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,36 @@ import os from "os"
import path from "path"
import fs from "fs/promises"

/**
* Generates random string
*
* @returns string
*/
export const generateRandString = () => (Math.random() * 36 * Date.now()).toString(36).replace(/\./g, ``)

/**
* Generates random temp filename
*
* @returns temp filename
*/
export const generateTEMP = () => path.join(os.tmpdir(), (Math.random() * 36 * Date.now()).toString(36).replace(/\./g, ``))
export const generateTEMP = () => path.join(os.tmpdir(), generateRandString())

/**
* i just lazy to write force and recursive
*
* @param filename filepath
*
* @returns
*/
export const removeTEMP = (filename: string) => fs.rm(filename, { force: true, recursive: true })

/**
* Saves buffer to temporary file
*
* @param data Buffer data
*
* @param ext File ext?
*
* @returns temp filename
*/
export const saveTEMP = async (data: Buffer, ext?: string) => {
Expand Down

1 comment on commit 3259843

@AliTahir2507
Copy link

Choose a reason for hiding this comment

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

Nice

Please sign in to comment.