Skip to content

Commit

Permalink
Merge pull request #2 from coldsurfers/feature/pick-files-util
Browse files Browse the repository at this point in the history
Feature/pick files util
  • Loading branch information
yungblud authored Jan 7, 2024
2 parents ead393a + 7471ca5 commit f54d5ff
Show file tree
Hide file tree
Showing 20 changed files with 800 additions and 666 deletions.
36 changes: 36 additions & 0 deletions packages/cloudinary-utils/.eslintrc.js
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',
},
}
3 changes: 2 additions & 1 deletion packages/cloudinary-utils/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist/
dist/
.eslintcache
6 changes: 6 additions & 0 deletions packages/cloudinary-utils/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": false,
"singleQuote": true
}
22 changes: 18 additions & 4 deletions packages/cloudinary-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@
},
"license": "MIT",
"devDependencies": {
"@coldsurfers/shared-utils": "1.0.0",
"@coldsurfers/shared-utils": "1.1.0",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"axios": "^1.6.3",
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-coldsurfers": "^1.1.3",
"eslint-config-prettier": "^8.10.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"form-data": "^4.0.0",
"fs": "^0.0.1-security",
"prettier": "^2.8.1",
"release-it": "^17.0.1",
"sha1": "^1.1.1",
"stream": "^0.0.2",
Expand All @@ -31,14 +42,17 @@
"prerelease": "yarn build",
"prerelease:rc": "yarn build",
"release": "release-it minor --ci",
"release:rc": "release-it --ci --preRelease=rc"
"release:rc": "release-it --ci --preRelease=rc",
"lint": "eslint --cache \"src/**/*.{js,jsx,ts,tsx}\"",
"lint:fix": "yarn lint --fix"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com"
},
"release-it": {
"git": {
"commitMessage": "chore: release @coldsurfers/cloudinary-utils v${version}"
"commitMessage": "chore: release @coldsurfers/cloudinary-utils v${version}",
"tagName": "cloudinary-utils-v${version}"
},
"github": {
"release": true
Expand All @@ -56,6 +70,6 @@
"util": "^0.12.5"
},
"dependencies": {
"@coldsurfers/shared-utils": "1.0.0"
"@coldsurfers/shared-utils": "1.1.0"
}
}
2 changes: 1 addition & 1 deletion packages/cloudinary-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './utils/cloudinary'
export * from './utils/cloudinary'
70 changes: 35 additions & 35 deletions packages/cloudinary-utils/src/utils/cloudinary.ts
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}&timestamp=${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}&timestamp=${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
}
1 change: 0 additions & 1 deletion packages/notion-utils/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = {
},
extends: [
"coldsurfers", // for nodejs-typescript, or 'coldsurfers/nodejs-typescript'
"coldsurfers/react-typescript", // for react-typescript
],
overrides: [
{
Expand Down
3 changes: 2 additions & 1 deletion packages/notion-utils/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
dist/
dist/
.eslintcache
6 changes: 6 additions & 0 deletions packages/notion-utils/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": false,
"singleQuote": true
}
9 changes: 6 additions & 3 deletions packages/notion-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@
"prerelease": "yarn build",
"prerelease:rc": "yarn build",
"release": "release-it minor --ci",
"release:rc": "release-it --ci --preRelease=rc"
"release:rc": "release-it --ci --preRelease=rc",
"lint": "eslint --cache \"src/**/*.{js,jsx,ts,tsx}\"",
"lint:fix": "yarn lint --fix"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com"
},
"release-it": {
"git": {
"commitMessage": "chore: release @coldsurfers/notion-utils v${version}"
"commitMessage": "chore: release @coldsurfers/notion-utils v${version}",
"tagName": "notion-utils-v${version}"
},
"github": {
"release": true
Expand All @@ -64,6 +67,6 @@
},
"dependencies": {
"@coldsurfers/cloudinary-utils": "1.0.1-rc.0",
"@coldsurfers/shared-utils": "1.0.1-rc.0"
"@coldsurfers/shared-utils": "1.1.0"
}
}
36 changes: 36 additions & 0 deletions packages/shared-utils/.eslintrc.js
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",
},
};
3 changes: 2 additions & 1 deletion packages/shared-utils/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist/
dist/
.eslintcache
6 changes: 6 additions & 0 deletions packages/shared-utils/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": false,
"singleQuote": true
}
20 changes: 17 additions & 3 deletions packages/shared-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coldsurfers/shared-utils",
"version": "1.0.1-rc.0",
"version": "1.1.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"source": "src/",
Expand All @@ -20,9 +20,22 @@
"prerelease": "yarn build",
"prerelease:rc": "yarn build",
"release": "release-it minor --ci",
"release:rc": "release-it --ci --preRelease=rc"
"release:rc": "release-it --ci --preRelease=rc",
"lint": "eslint --cache \"src/**/*.{js,jsx,ts,tsx}\"",
"lint:fix": "yarn lint --fix"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-coldsurfers": "^1.1.3",
"eslint-config-prettier": "^8.10.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"prettier": "^2.8.1",
"release-it": "^17.0.1",
"typescript": "^5.3.3"
},
Expand All @@ -31,7 +44,8 @@
},
"release-it": {
"git": {
"commitMessage": "chore: release @coldsurfers/cloudinary-utils v${version}"
"commitMessage": "chore: release @coldsurfers/cloudinary-utils v${version}",
"tagName": "shared-utils-v${version}"
},
"github": {
"release": true
Expand Down
3 changes: 2 additions & 1 deletion packages/shared-utils/src/index.ts
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'
22 changes: 11 additions & 11 deletions packages/shared-utils/src/libs/generateUUID.ts
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)
})
}
6 changes: 3 additions & 3 deletions packages/shared-utils/src/libs/getRandomInt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @returns {number} - A random integer between `min` and `max`, inclusive.
*/
export default function getRandomInt(minimum: number, maximum: number) {
const min = Math.ceil(minimum);
const max = Math.floor(maximum);
return Math.floor(Math.random() * (max - min + 1)) + min;
const min = Math.ceil(minimum)
const max = Math.floor(maximum)
return Math.floor(Math.random() * (max - min + 1)) + min
}
Loading

0 comments on commit f54d5ff

Please sign in to comment.