Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alpheustangs committed May 9, 2024
0 parents commit 12ead17
Show file tree
Hide file tree
Showing 77 changed files with 5,510 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Set text files to use LF line endings
* text eol=lf
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "daily"
time: "00:00"
timezone: "Etc/UTC"
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Caches
__MACOSX/
.DS_Store

# Builds
dist/
.media/

# Modules
node_modules/

# Logs
*.log
pnpm-debug.log*
3 changes: 3 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
auto-install-peers=true
strict-peer-dependencies=true
engine-strict=true
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024-present, Alpheus Tang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
118 changes: 118 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# FileGo

Solution for splitting, checking, and merging files.

## Install

For TypeScript / JavaScript environment:

```bash
npm i @filego/ts
```

For Node environment:

```bash
npm i @filego/node
```

## Usage Of `@filego/ts`

Usage of different functions in `@filego/ts`:

#### `split`

Split files into chunks, and the chunks can be used to check and merge later. It will return the `fileSize`, `totalChunks` and the `chunks`.

```typescript
import { split } from "@filego/ts";

await split({
file: File,
chunkSize: 1 * 1024 * 1024, // 1MB
});
```

#### `check`

Check file integrity by verifying the `chunks` with `fileSize` and `totalChunks` parameters. It will return the `status` and the `error` of the check.

```typescript
import { check } from "@filego/ts";

await check({
chunks: [ /* ... */ ],
fileSize: 123456789,
totalChunks: 10,
});
```

#### `merge`

Merge the chunks by using the `chunks` parameters. It will return the `blob` and `buffer` of the merged file.

```typescript
import { merge } from "@filego/ts";

await merge({
chunks: [ /* ... */ ],
});
```

## Usage Of `@filego/node`

Usage of different functions in `@filego/node`:

#### `split`

Split files from a file path to a directory directly. It will only return the `fileSize` and the `totalChunks` of the file.

```typescript
import { split } from "@filego/node";

await split({
inPath: "/path/to/file.txt",
outDir: "/path/to/dir",
chunkSize: 1 * 1024 * 1024,
});
```

#### `check`

Check file integrity by verifying the the chunks specified in the `inDir` with `fileSize`, `totalChunks` parameters. It will return the `status` and the `error` of the check.

```typescript
import { check } from "@filego/node";

await check({
inDir: "/path/to/dir",
fileSize: 123456789,
totalChunks: 10,
});
```

#### `merge`

Merge the chunks from a directory to a specified path directly. Therefore, nothing will be returned.

```typescript
import { merge } from "@filego/node";

await merge({
inDir: "/path/to/dir",
outPath: "/path/to/file.txt",
});
```

## Examples

Check out the Web and API examples.

| Direcotry | Description | Link |
| --------- | ----------------------- | ------------------------ |
| Web | Web example in React | [web](./examples/web/) |
| Node | Node example in Fastify | [node](./examples/node/) |

## License

This project is MIT licensed, you can find the license file [here](./LICENSE).
84 changes: 84 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
"files": {
"ignore": [
"./.git/**/*",
"./**/__MACOSX/**/*",
"./**/.DS_Store",
"./**/node_modules/**/*",
"./**/dist/**/*",
"./pnpm-lock.yaml"
],
"ignoreUnknown": true
},
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"style": {
"noInferrableTypes": "off"
}
}
},
"formatter": {
"enabled": true,
"formatWithErrors": false,
"indentStyle": "space",
"indentWidth": 4,
"lineEnding": "lf",
"lineWidth": 80
},
"organizeImports": {
"enabled": true
},
"javascript": {
"parser": {
"unsafeParameterDecoratorsEnabled": true
},
"formatter": {
"quoteStyle": "double",
"jsxQuoteStyle": "double",
"quoteProperties": "asNeeded",
"trailingComma": "all",
"semicolons": "always",
"arrowParentheses": "always",
"enabled": true,
"indentStyle": "space",
"indentWidth": 4,
"lineEnding": "lf",
"lineWidth": 80,
"bracketSameLine": false,
"bracketSpacing": true
}
},
"json": {
"parser": {
"allowComments": false,
"allowTrailingCommas": false
},
"formatter": {
"enabled": true,
"indentStyle": "space",
"indentWidth": 4,
"lineEnding": "lf",
"lineWidth": 80,
"trailingCommas": "none"
}
},
"overrides": [
{
"include": ["./**/tsconfig.base.json", "./**/tsconfig.json"],
"json": {
"parser": {
"allowComments": true,
"allowTrailingCommas": true
}
}
}
]
}
1 change: 1 addition & 0 deletions examples/node/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT="4001"
1 change: 1 addition & 0 deletions examples/node/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT="4000"
22 changes: 22 additions & 0 deletions examples/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"private": true,
"scripts": {
"dev": "stormode dev",
"build": "tsc && stormode build",
"preview": "stormode preview"
},
"dependencies": {
"@fastify/cors": "^9.0.1",
"@fastify/multipart": "^8.2.0",
"@filego/node": "workspace:^",
"fastify": "^4.26.2",
"fs-extra": "^11.2.0",
"stormode-terminal": "^0.2.0"
},
"devDependencies": {
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.12.10",
"stormode": "^0.5.2",
"typescript": "^5.4.5"
}
}
14 changes: 14 additions & 0 deletions examples/node/src/configs/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as path from "node:path";

const port: number = Number(process.env.PORT);

const isDev: boolean = process.env.NODE_ENV === "development";
const isPrd: boolean = process.env.NODE_ENV === "production";

const cwd: string = process.cwd();
const root: string = path.resolve(cwd);

const uploadRoot: string = path.join(root, ".media", "uploads");
const cacheRoot: string = path.join(root, ".media", "cache");

export { port, isDev, isPrd, root, uploadRoot, cacheRoot };
79 changes: 79 additions & 0 deletions examples/node/src/controllers/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import type { MultipartValue } from "@fastify/multipart";
import type { CheckResult } from "@filego/node";
import type { FastifyReply, FastifyRequest } from "fastify";

import * as path from "node:path";

import { check } from "@filego/node";

import { cacheRoot, isDev } from "#/configs/env";

import { terminal } from "#/utils/terminal";

const checkFile = async (
request: FastifyRequest<{
Body?: {
id?: MultipartValue;
fileSize?: MultipartValue;
totalChunks?: MultipartValue;
};
}>,
reply: FastifyReply,
): Promise<FastifyReply> => {
try {
if (
!request.body ||
!request.body.id ||
!request.body.fileSize ||
!request.body.totalChunks
) {
return reply.code(400).send({
status: "error",
message: "blank",
});
}

const id: string = request.body.id.value as string;
const fileSize: number = Number.parseInt(
request.body.fileSize.value as string,
);
const totalChunks: number = Number.parseInt(
request.body.totalChunks.value as string,
);

if (Number.isNaN(fileSize) || Number.isNaN(totalChunks)) {
return reply.code(400).send({
status: "error",
message: "invalid",
});
}

const result: CheckResult = await check({
fileSize,
totalChunks,
inDir: path.join(cacheRoot, id),
});

if (result.status === "error") {
return reply.code(400).send({
status: "error",
message: result.error,
data: {
missing: result.missing,
},
});
}

return reply.code(200).send({
status: "success",
});
} catch (e: unknown) {
isDev && terminal.error(e);
return reply.code(500).send({
status: "error",
message: "server",
});
}
};

export { checkFile };
Loading

0 comments on commit 12ead17

Please sign in to comment.