-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from coldsurfers/feature/pick-files-util
Feature/pick files util
- Loading branch information
Showing
20 changed files
with
800 additions
and
666 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module.exports = { | ||
env: { | ||
browser: true, | ||
es2021: true, | ||
node: true, | ||
}, | ||
extends: [ | ||
'coldsurfers', // for nodejs-typescript, or 'coldsurfers/nodejs-typescript' | ||
], | ||
overrides: [ | ||
{ | ||
env: { | ||
node: true, | ||
}, | ||
files: ['.eslintrc.{js,cjs}'], | ||
parserOptions: { | ||
sourceType: 'script', | ||
}, | ||
}, | ||
], | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
}, | ||
rules: { | ||
'react/jsx-filename-extension': 'off', | ||
'react/jsx-props-no-spreading': 'off', | ||
'react/prop-types': [0], | ||
'no-bitwise': 'off', | ||
camelcase: 'off', | ||
'no-param-reassign': 'off', | ||
'no-await-in-loop': 'off', | ||
'no-return-await': 'off', | ||
'react/no-array-index-key': 'off', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
dist/ | ||
dist/ | ||
.eslintcache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"trailingComma": "es5", | ||
"tabWidth": 2, | ||
"semi": false, | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from './utils/cloudinary' | ||
export * from './utils/cloudinary' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,58 @@ | ||
import fs from "fs"; | ||
import sha1 from "sha1"; | ||
import axios from "axios"; | ||
import util from "util"; | ||
import stream from "stream"; | ||
import FormData from "form-data"; | ||
import { generateUUID } from "@coldsurfers/shared-utils"; | ||
import fs from 'fs' | ||
import sha1 from 'sha1' | ||
import axios from 'axios' | ||
import util from 'util' | ||
import stream from 'stream' | ||
import FormData from 'form-data' | ||
import { generateUUID } from '@coldsurfers/shared-utils' | ||
|
||
const pipeline = util.promisify(stream.pipeline); | ||
const pipeline = util.promisify(stream.pipeline) | ||
|
||
export const generateCloudinaryUploadSignature = (filename: string) => { | ||
const timestamp = Date.now(); | ||
const directory = "blog-assets"; | ||
const public_id = `${directory}/${filename}`; | ||
const payload_to_sign = `public_id=${public_id}×tamp=${timestamp}`; | ||
const api_secret = process.env.CLOUDINARY_API_SECRET; | ||
const signature = sha1(payload_to_sign + api_secret); | ||
const timestamp = Date.now() | ||
const directory = 'blog-assets' | ||
const public_id = `${directory}/${filename}` | ||
const payload_to_sign = `public_id=${public_id}×tamp=${timestamp}` | ||
const api_secret = process.env.CLOUDINARY_API_SECRET | ||
const signature = sha1(payload_to_sign + api_secret) | ||
return { | ||
signature, | ||
public_id, | ||
timestamp, | ||
}; | ||
}; | ||
} | ||
} | ||
|
||
export const uploadCloudinary = async (url: string) => { | ||
const request = await axios.get(url, { | ||
responseType: "stream", | ||
}); | ||
if (!fs.existsSync("temps")) { | ||
fs.mkdirSync("temps"); | ||
responseType: 'stream', | ||
}) | ||
if (!fs.existsSync('temps')) { | ||
fs.mkdirSync('temps') | ||
} | ||
const filepath = `temps/${generateUUID()}`; | ||
await pipeline(request.data, fs.createWriteStream(filepath)); | ||
const filepath = `temps/${generateUUID()}` | ||
await pipeline(request.data, fs.createWriteStream(filepath)) | ||
const { signature, public_id, timestamp } = generateCloudinaryUploadSignature( | ||
generateUUID() | ||
); | ||
) | ||
|
||
const cloudinarySignedUploadAPI = | ||
"https://api.cloudinary.com/v1_1/druidbphk/image/upload"; | ||
const formdata = new FormData(); | ||
'https://api.cloudinary.com/v1_1/druidbphk/image/upload' | ||
const formdata = new FormData() | ||
|
||
formdata.append("file", fs.createReadStream(filepath)); | ||
formdata.append("public_id", public_id); | ||
formdata.append("signature", signature); | ||
formdata.append("api_key", process.env.CLOUDINARY_API_KEY); | ||
formdata.append("timestamp", timestamp); | ||
formdata.append('file', fs.createReadStream(filepath)) | ||
formdata.append('public_id', public_id) | ||
formdata.append('signature', signature) | ||
formdata.append('api_key', process.env.CLOUDINARY_API_KEY) | ||
formdata.append('timestamp', timestamp) | ||
|
||
const { data } = await axios({ | ||
method: "post", | ||
method: 'post', | ||
url: cloudinarySignedUploadAPI, | ||
headers: formdata.getHeaders(), | ||
data: formdata, | ||
}); | ||
}) | ||
if (fs.existsSync(filepath)) { | ||
fs.unlinkSync(filepath); | ||
fs.unlinkSync(filepath) | ||
} | ||
return data; | ||
}; | ||
return data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules/ | ||
dist/ | ||
dist/ | ||
.eslintcache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"trailingComma": "es5", | ||
"tabWidth": 2, | ||
"semi": false, | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module.exports = { | ||
env: { | ||
browser: true, | ||
es2021: true, | ||
node: true, | ||
}, | ||
extends: [ | ||
"coldsurfers", // for nodejs-typescript, or 'coldsurfers/nodejs-typescript' | ||
], | ||
overrides: [ | ||
{ | ||
env: { | ||
node: true, | ||
}, | ||
files: [".eslintrc.{js,cjs}"], | ||
parserOptions: { | ||
sourceType: "script", | ||
}, | ||
}, | ||
], | ||
parserOptions: { | ||
ecmaVersion: "latest", | ||
sourceType: "module", | ||
}, | ||
rules: { | ||
"react/jsx-filename-extension": "off", | ||
"react/jsx-props-no-spreading": "off", | ||
"react/prop-types": [0], | ||
"no-bitwise": "off", | ||
camelcase: "off", | ||
"no-param-reassign": "off", | ||
"no-await-in-loop": "off", | ||
"no-return-await": "off", | ||
"react/no-array-index-key": "off", | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
dist/ | ||
dist/ | ||
.eslintcache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"trailingComma": "es5", | ||
"tabWidth": 2, | ||
"semi": false, | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as generateUUID } from './libs/generateUUID' | ||
export { default as getRandomInt } from './libs/getRandomInt' | ||
export { default as getRandomInt } from './libs/getRandomInt' | ||
export { default as pickFile } from './libs/pickFile' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
export default function generateUUID() { | ||
// Public Domain/MIT | ||
let d = new Date().getTime(); // Timestamp | ||
let d = new Date().getTime() // Timestamp | ||
let d2 = | ||
(typeof performance !== "undefined" && | ||
(typeof performance !== 'undefined' && | ||
performance.now && | ||
performance.now() * 1000) || | ||
0; // Time in microseconds since page-load or 0 if unsupported | ||
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => { | ||
let r = Math.random() * 16; // random number between 0 and 16 | ||
0 // Time in microseconds since page-load or 0 if unsupported | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { | ||
let r = Math.random() * 16 // random number between 0 and 16 | ||
if (d > 0) { | ||
// Use timestamp until depleted | ||
r = (d + r) % 16 | 0; | ||
d = Math.floor(d / 16); | ||
r = (d + r) % 16 | 0 | ||
d = Math.floor(d / 16) | ||
} else { | ||
// Use microseconds since page-load if supported | ||
r = (d2 + r) % 16 | 0; | ||
d2 = Math.floor(d2 / 16); | ||
r = (d2 + r) % 16 | 0 | ||
d2 = Math.floor(d2 / 16) | ||
} | ||
return (c === "x" ? r : (r & 0x3) | 0x8).toString(16); | ||
}); | ||
return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.