-
Notifications
You must be signed in to change notification settings - Fork 9
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
14 changed files
with
364 additions
and
51 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ data | |
.env | ||
node_modules | ||
.github | ||
database.json |
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ node_modules | |
data | ||
.env | ||
build | ||
database.json |
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,3 +1,8 @@ | ||
# blobstr | ||
|
||
A http file server with a nostr twist | ||
Generic blob storage and retrieval for nostr | ||
|
||
## TODO | ||
|
||
- verify auth events | ||
- write up simple API spec |
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
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,49 @@ | ||
import dayjs from "dayjs"; | ||
import { JSONFilePreset } from "lowdb/node"; | ||
import path from "node:path"; | ||
|
||
type DBSchema = { | ||
blobs: Record<string, { expiration?: number; pubkeys?: string[]; created: number; mimeType?: string; size?: number }>; | ||
}; | ||
const db = await JSONFilePreset<DBSchema>(path.join(process.cwd(), "database.json"), { | ||
blobs: {}, | ||
}); | ||
db.data.blobs = db.data.blobs || {}; | ||
setInterval(() => db.write(), 1000); | ||
|
||
export function hasBlobEntry(hash: string) { | ||
return !!db.data.blobs[hash]; | ||
} | ||
export function getOrCreateBlobEntry(hash: string) { | ||
let blob = db.data.blobs[hash]; | ||
if (!blob) blob = db.data.blobs[hash] = { created: dayjs().unix() }; | ||
return blob; | ||
} | ||
export function setBlobExpiration(hash: string, expiration: number) { | ||
let blob = getOrCreateBlobEntry(hash); | ||
|
||
if (blob.expiration) blob.expiration = Math.max(blob.expiration, expiration); | ||
else blob.expiration = expiration; | ||
} | ||
export function setBlobMimetype(hash: string, mimeType: string) { | ||
let blob = getOrCreateBlobEntry(hash); | ||
blob.mimeType = mimeType; | ||
} | ||
export function setBlobSize(hash: string, size: number) { | ||
let blob = getOrCreateBlobEntry(hash); | ||
blob.size = size; | ||
} | ||
export function addPubkeyToBlob(hash: string, pubkey: string) { | ||
let blob = getOrCreateBlobEntry(hash); | ||
if (blob.pubkeys) { | ||
if (!blob.pubkeys.includes(pubkey)) blob.pubkeys.push(pubkey); | ||
} else blob.pubkeys = [pubkey]; | ||
} | ||
export function removePubkeyFromBlob(hash: string, pubkey: string) { | ||
let blob = getOrCreateBlobEntry(hash); | ||
if (blob.pubkeys) { | ||
if (blob.pubkeys.includes(pubkey)) blob.pubkeys.splice(blob.pubkeys.indexOf(pubkey), 1); | ||
} | ||
} | ||
|
||
export { db }; |
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.