Skip to content

Commit

Permalink
feat: Deno 2.0
Browse files Browse the repository at this point in the history
Replaces wren with hono
Upgrades standard library packages to their latest versions
Adds PORT env variable
Uses JSR as main source of packages

Signed-off-by: Amy <amy@amogus.cloud>
  • Loading branch information
amycatgirl committed Oct 21, 2024
1 parent 3b605a1 commit 28260b9
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 193 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# tempstore

no nonsense, self-hostable temporary file service

## running

create a folder called `tmpst` at the root of this repository, then run

```
```bash
DOMAIN="https://example.com" just run
```

or
or

```
```bash
DOMAIN="https://example.com" deno task start
```

Expand Down Expand Up @@ -40,6 +41,7 @@ method: `POST`
content Type: `multipart/form-data`

body

- `file`: self-explanatory
- `until`: Amount of time to store the file for (in hours)

Expand Down
16 changes: 12 additions & 4 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"importMap": "./import_map.json",
"tasks": {
"start": "deno run -A -r main.ts"
}
"tasks": {
"start": "deno run -A -r main.ts"
},
"imports": {
"http": "jsr:@std/http@^1.0.8",
"ulid": "jsr:@std/ulid@^1.0.0",
"path": "jsr:@std/path@^1.0.6",
"dotenv": "jsr:@std/dotenv@^0.225.2",
"fs": "jsr:@std/fs@^1.0.4",
"eta": "jsr:@eta-dev/eta@^3.5.0",
"hono": "jsr:@hono/hono@^4.6.5"
}
}
81 changes: 80 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions env.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { load } from "dotenv/mod.ts"
import { load } from "dotenv";

export const env = await load();

export const env = await load()
15 changes: 8 additions & 7 deletions eta.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Eta } from "eta/src/index.ts";
import { resolve } from "path/mod.ts";
import { Eta } from "eta";
import { resolve } from "path";

const eta = new Eta({
views: resolve("./templates"),
cache: true,
useWith: true,
})
views: resolve("./templates"),
cache: true,
useWith: true,
});

export { eta };

export { eta }
11 changes: 0 additions & 11 deletions import_map.json

This file was deleted.

37 changes: 21 additions & 16 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import { serve } from 'wren/mod.ts';
import CreateObjectRoute from "./routes/create_object.ts"
import GetObjectRoute from "./routes/get_object.ts";
import { Hono } from "hono";

import { StorageSingleton } from "./storage.ts";
import IndexRoute from "./routes/index_route.ts";
import UICreateObjectRoute from "./routes/ui_create_object_route.ts";

const storage = StorageSingleton.getInstance()
import ApiRoutes from "./routes/api/index.ts";
import Views from "./routes/views/index.ts";
import { env } from "./env.ts";

const app = new Hono();

const storage = StorageSingleton.getInstance();

const routes = [
CreateObjectRoute,
GetObjectRoute,
IndexRoute,
UICreateObjectRoute
];
await storage.removeLeftovers();

await storage.removeLeftovers()
app.route("/", Views);
app.route("/api", ApiRoutes);

setInterval(() => {
storage.manager.purgeExpired()
}, 1000)
storage.manager.purgeExpired();
}, 1000);

serve(routes);
Deno.serve(
{
port: parseInt(env["PORT"]) || 5544,
hostname: env["DOMAIN"] || "127.0.0.1",
},
app.fetch,
);

// Run on different port
// serve(routes, { port: 3000 });
81 changes: 81 additions & 0 deletions routes/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Hono, type Context } from "hono";

import { StorageSingleton } from "../../storage.ts";

const api = new Hono();

api.post("/object", async (c: Context) => {
const form = await c.req.formData();
const file = form.get("file") ?? 1;
const until: unknown = form.get("until");
const storage = StorageSingleton.getInstance();

if (until && (until as number) > 5) {
return c.json(
{
message: "You can't store files for more than five hours!",
},
400,
);
}

if (!file) {
return c.json({
message: "Missing file",
});
} else if (file instanceof File) {
try {
const { id, filename, stored_until } = await storage.storeFile(
file,
(until as number | undefined) ?? 1,
);

console.log(`[CREATE] File ${filename} was created`);
return c.json(
{
id,
filename,
message: `Saved ${filename} until ${new Date(stored_until)}`,
},
201,
);
} catch {
return c.json({ message: "Error whilst creating object" }, 500);
}
} else {
return c.json({}, 406);
}
});

api.get("/object/:id", async (c: Context) => {
if (!c.req.param()) return c.text("", 400);

const objectId = c.req.param("id");
const storage = StorageSingleton.getInstance();
const object = storage.getFile(objectId);

if (!object) return c.json({ message: `Could not find object ${objectId}` });

console.log(`[GET] Found ${object.filename}, serving...`);

try {
const file = await object.openFile();

if (!file) throw new Error(`Could not open file ${object.filename}`);

c.header("Content-Type", object.mimeType);
c.status(200);

return c.body(file, { "Content-type": object.mimeType });
} catch (error: unknown) {
return c.json(
{
message: "Error whilst opening file",
error: (error as Error).message,
},
500,
);
}
});

export default api;
45 changes: 0 additions & 45 deletions routes/create_object.ts

This file was deleted.

33 changes: 0 additions & 33 deletions routes/get_object.ts

This file was deleted.

Loading

0 comments on commit 28260b9

Please sign in to comment.