-
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.
- Loading branch information
Showing
8 changed files
with
581 additions
and
46 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -28,5 +28,6 @@ export default { | |
"sharp", | ||
"winston", | ||
"zod", | ||
"glob", | ||
], | ||
}; |
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,91 @@ | ||
import * as fs from "fs/promises"; | ||
import ansis from "ansis"; | ||
import { confirm } from "@inquirer/prompts"; | ||
import { glob } from "glob"; | ||
|
||
import { logger } from "./logger"; | ||
import { getConfig } from "./config"; | ||
import { uploadFilesBulk } from "./s3"; | ||
|
||
type UploadPathOptions = { | ||
force?: boolean; | ||
showFiles?: boolean; | ||
}; | ||
|
||
export const uploadPath = async ( | ||
src: string, | ||
dest: string, | ||
options: UploadPathOptions | ||
) => { | ||
const { force = false, showFiles = false } = options; | ||
const config = getConfig(); | ||
const { bucketName } = config; | ||
|
||
const { filePaths, totalSize } = await findFilesAndSize(src); | ||
|
||
if (filePaths.length === 0) { | ||
logger.info("No files to upload from that path"); | ||
return; | ||
} | ||
|
||
logger.info("Number of items to upload: %o", filePaths.length); | ||
logger.info("Total size to upload: %s", formatBytes(totalSize)); | ||
|
||
if (showFiles) { | ||
for (const filePath of filePaths) { | ||
logger.info(ansis.gray(` - ${filePath}`)); | ||
} | ||
} | ||
|
||
if (!force) { | ||
const confirmUpload = await confirm({ | ||
message: `Are you sure you want to upload ${ | ||
filePaths.length | ||
} items (${formatBytes(totalSize)})?`, | ||
}); | ||
|
||
if (!confirmUpload) { | ||
logger.info("Upload aborted by the user."); | ||
return; | ||
} | ||
} | ||
|
||
try { | ||
await uploadFilesBulk(bucketName, src, dest, filePaths); | ||
logger.info(ansis.green("Done!")); | ||
} catch (error) { | ||
logger.error("Error occurred while uploading files:", error); | ||
} | ||
}; | ||
|
||
async function findFilesAndSize( | ||
src: string | ||
): Promise<{ filePaths: string[]; totalSize: number }> { | ||
const filePaths = await glob("**/*", { | ||
cwd: src, | ||
nodir: true, | ||
absolute: true, | ||
}); | ||
|
||
let totalSize = 0; | ||
await Promise.all( | ||
filePaths.map(async (filePath) => { | ||
const stats = await fs.stat(filePath); | ||
totalSize += stats.size; | ||
}) | ||
); | ||
|
||
return { filePaths, totalSize }; | ||
} | ||
|
||
export function formatBytes(bytes: number, decimals = 2) { | ||
if (bytes === 0) return "0 Bytes"; | ||
|
||
const k = 1024; | ||
const dm = decimals < 0 ? 0 : decimals; | ||
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; | ||
|
||
const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
|
||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i]; | ||
} |
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
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,24 @@ | ||
export const deriveContentType = (format: string | undefined) => { | ||
switch (format?.toLowerCase()) { | ||
case "jpeg": | ||
case "jpg": | ||
return "image/jpeg"; | ||
case "png": | ||
return "image/png"; | ||
case "webp": | ||
return "image/webp"; | ||
case "gif": | ||
return "image/gif"; | ||
case "svg": | ||
return "image/svg+xml"; | ||
case "tiff": | ||
case "tif": | ||
return "image/tiff"; | ||
case "avif": | ||
return "image/avif"; | ||
case "pdf": | ||
return "application/pdf"; | ||
default: | ||
return "application/octet-stream"; | ||
} | ||
}; |