upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkieran <kieran@harkin.me>2024-05-14 13:59:59 +0100
committerkieran <kieran@harkin.me>2024-05-20 21:38:44 +0100
commit8d6d58871542dfcbb3f9d5d610a13a1e17358e58 (patch)
tree7c68c309b5bb4f7b69a2eb417321022a5ed64d42
parent67e870d95a5eb48329f77dc7c8ffc64a2b139019 (diff)
nip96 list uploads
-rw-r--r--96.md108
1 files changed, 68 insertions, 40 deletions
diff --git a/96.md b/96.md
index f7d901f..4203b63 100644
--- a/96.md
+++ b/96.md
@@ -84,46 +84,43 @@ it must use the "api_url" field instead.
84 84
85See https://github.com/aljazceru/awesome-nostr#nip-96-file-storage-servers. 85See https://github.com/aljazceru/awesome-nostr#nip-96-file-storage-servers.
86 86
87
88## Auth
89
90When indicated, `clients` must add an [NIP-98](98.md) `Authorization` header (**optionally** with the encoded `payload` tag set to the base64-encoded 256-bit SHA-256 hash of the file - not the hash of the whole request body).
91
87## Upload 92## Upload
88 93
89A file can be uploaded one at a time to `https://your-file-server.example/custom-api-path` (route from `https://your-file-server.example/.well-known/nostr/nip96.json` "api_url" field) as `multipart/form-data` content type using `POST` method with the file object set to the `file` form data field. 94`POST $api_url` as `multipart/form-data`.
90 95
91`Clients` must add an [NIP-98](98.md) `Authorization` header (**optionally** with the encoded `payload` tag set to the base64-encoded 256-bit SHA-256 hash of the file - not the hash of the whole request body). 96**AUTH required**
92If using an html form, use an `Authorization` form data field instead.
93 97
94These following **optional** form data fields MAY be used by `servers` and SHOULD be sent by `clients`: 98List of form fields:
95- `expiration`: string of the UNIX timestamp in seconds. Empty string if file should be stored forever. The server isn't required to honor this; 99- `file`: **REQUIRED** the file to upload
96- `size`: string of the file byte size. This is just a value the server can use to reject early if the file size exceeds the server limits; 100- `caption`: **RECOMMENDED** loose description;
97- `alt`: (recommended) strict description text for visibility-impaired users; 101- `expiration`: UNIX timestamp in seconds. Empty string if file should be stored forever. The server isn't required to honor this.
98- `caption`: loose description; 102- `size`: File byte size. This is just a value the server can use to reject early if the file size exceeds the server limits.
99- `media_type`: "avatar" or "banner". Informs the server if the file will be used as an avatar or banner. If absent, the server will interpret it as a normal upload, without special treatment; 103- `alt`: **RECOMMENDED** strict description text for visibility-impaired users.
104- `media_type`: "avatar" or "banner". Informs the server if the file will be used as an avatar or banner. If absent, the server will interpret it as a normal upload, without special treatment.
100- `content_type`: mime type such as "image/jpeg". This is just a value the server can use to reject early if the mime type isn't supported. 105- `content_type`: mime type such as "image/jpeg". This is just a value the server can use to reject early if the mime type isn't supported.
101 106
102
103Others custom form data fields may be used depending on specific `server` support. 107Others custom form data fields may be used depending on specific `server` support.
104The `server` isn't required to store any metadata sent by `clients`. 108The `server` isn't required to store any metadata sent by `clients`.
105 109
106Note for `clients`: if using an HTML form, it is important for the `file` form field to be the **last** one, or be re-ordered right before sending or be appended as the last field of XHR2's FormData object.
107
108The `filename` embedded in the file may not be honored by the `server`, which could internally store just the SHA-256 hash value as the file name, ignoring extra metadata. 110The `filename` embedded in the file may not be honored by the `server`, which could internally store just the SHA-256 hash value as the file name, ignoring extra metadata.
109The hash is enough to uniquely identify a file, that's why it will be used on the "download" and "delete" routes. 111The hash is enough to uniquely identify a file, that's why it will be used on the `download` and `delete` routes.
110
111The `server` MUST link the user's `pubkey` string (which is embedded in the decoded header value) as the owner of the file so to later allow them to delete the file.
112Note that if a file with the same hash of a previously received file (so the same file) is uploaded by another user, the server doesn't need to store the new file.
113It should just add the new user's `pubkey` to the list of the owners of the already stored file with said hash (if it wants to save space by keeping just one copy of the same file, because multiple uploads of the same file results in the same file hash).
114 112
115The `server` MAY also store the `Authorization` header/field value (decoded or not) for accountability purpose as this proves that the user with the unique pubkey did ask for the upload of the file with a specific hash. However, storing the pubkey is sufficient to establish ownership. 113The `server` MUST link the user's `pubkey` string as the owner of the file so to later allow them to delete the file.
116 114
117The `server` MUST reject with 413 Payload Too Large if file size exceeds limits. 115### Response codes
118 116
119The `server` MUST reject with 400 Bad Request status if some fields are invalid. 117- `200 OK`: File upload exists, but is successful (Existing hash)
120 118- `201 Created`: File upload successful (New hash)
121The `server` MUST reply to the upload with 200 OK status if the `payload` tag value contains an already used SHA-256 hash (if file is already owned by the same pubkey) or reject the upload with 403 Forbidden status if it isn't the same of the received file. 119- `202 Accepted`: File upload is awaiting processing, see [Delayed Processing](#delayed-processing) section
122 120- `413 Payload Too Large`: File size exceeds limit
123The `server` MAY reject the upload with 402 Payment Required status if the user has a pending payment (Payment flow is not strictly required. Server owners decide if the storage is free or not. Monetization schemes may be added later to correlated NIPs.). 121- `400 Bad Request`: Form data is invalid or not supported.
124 122- `403 Forbidden`: User is not allowed to upload or the uploaded file hash didnt match the hash included in the `Authorization` header `payload` tag.
125On successful uploads the `server` MUST reply with **201 Created** HTTP status code or **202 Accepted** if a `processing_url` field is added 123- `402 Payment Required`: Payment is required by the server, **this flow is undefined**.
126to the response so that the `client` can follow the processing status (see [Delayed Processing](#delayed-processing) section).
127 124
128The upload response is a json object as follows: 125The upload response is a json object as follows:
129 126
@@ -179,11 +176,13 @@ The upload response is a json object as follows:
179 176
180Note that if the server didn't apply any transformation to the received file, both `nip94_event.tags.*.ox` and `nip94_event.tags.*.x` fields will have the same value. The server MUST link the saved file to the SHA-256 hash of the **original** file before any server transformations (the `nip94_event.tags.*.ox` tag value). The **original** file's SHA-256 hash will be used to identify the saved file when downloading or deleting it. 177Note that if the server didn't apply any transformation to the received file, both `nip94_event.tags.*.ox` and `nip94_event.tags.*.x` fields will have the same value. The server MUST link the saved file to the SHA-256 hash of the **original** file before any server transformations (the `nip94_event.tags.*.ox` tag value). The **original** file's SHA-256 hash will be used to identify the saved file when downloading or deleting it.
181 178
182`Clients` may upload the same file to one or many `servers`. 179`clients` may upload the same file to one or many `servers`.
183After successful upload, the `client` may optionally generate and send to any set of nostr `relays` a [NIP-94](94.md) event by including the missing fields. 180After successful upload, the `client` may optionally generate and send to any set of nostr `relays` a [NIP-94](94.md) event by including the missing fields.
184 181
185Alternatively, instead of using NIP-94, the `client` can share or embed on a nostr note just the above url. 182Alternatively, instead of using NIP-94, the `client` can share or embed on a nostr note just the above url.
186 183
184`clients` may also use the tags from the `nip94_event` to construct an `imeta` tag
185
187### Delayed Processing 186### Delayed Processing
188 187
189Sometimes the server may want to place the uploaded file in a processing queue for deferred file processing. 188Sometimes the server may want to place the uploaded file in a processing queue for deferred file processing.
@@ -219,7 +218,7 @@ However, for all file actions, such as download and deletion, the **original** f
219 218
220## Download 219## Download
221 220
222`Servers` must make available the route `https://your-file-server.example/custom-api-path/<sha256-file-hash>(.ext)` (route taken from `https://your-file-server.example/.well-known/nostr/nip96.json` "api_url" or "download_url" field) with `GET` method for file download. 221`GET $api_url/<sha256-hash>(.ext)`
223 222
224The primary file download url informed at the upload's response field `nip94_event.tags.*.url` 223The primary file download url informed at the upload's response field `nip94_event.tags.*.url`
225can be that or not (it can be any non-standard url the server wants). 224can be that or not (it can be any non-standard url the server wants).
@@ -227,17 +226,17 @@ If not, the server still MUST also respond to downloads at the standard url
227mentioned on the previous paragraph, to make it possible for a client 226mentioned on the previous paragraph, to make it possible for a client
228to try downloading a file on any NIP-96 compatible server by knowing just the SHA-256 file hash. 227to try downloading a file on any NIP-96 compatible server by knowing just the SHA-256 file hash.
229 228
230Note that the "\<sha256-file-hash\>" part is from the **original** file, **not** from the **transformed** file if the uploaded file went through any server transformation. 229Note that the "\<sha256-hash\>" part is from the **original** file, **not** from the **transformed** file if the uploaded file went through any server transformation.
231 230
232Supporting ".ext", meaning "file extension", is required for `servers`. It is optional, although recommended, for `clients` to append it to the path. 231Supporting ".ext", meaning "file extension", is required for `servers`. It is optional, although recommended, for `clients` to append it to the path.
233When present it may be used by `servers` to know which `Content-Type` header to send (e.g.: "Content-Type": "image/png" for ".png" extension). 232When present it may be used by `servers` to know which `Content-Type` header to send (e.g.: "Content-Type": "image/png" for ".png" extension).
234The file extension may be absent because the hash is the only needed string to uniquely identify a file. 233The file extension may be absent because the hash is the only needed string to uniquely identify a file.
235 234
236Example: `https://your-file-server.example/custom-api-path/719171db19525d9d08dd69cb716a18158a249b7b3b3ec4bbdec5698dca104b7b.png` 235Example: `$api_url/719171db19525d9d08dd69cb716a18158a249b7b3b3ec4bbdec5698dca104b7b.png`
237 236
238### Media Transformations 237### Media Transformations
239 238
240`Servers` may respond to some media transformation query parameters and ignore those they don't support by serving 239`servers` may respond to some media transformation query parameters and ignore those they don't support by serving
241the original media file without transformations. 240the original media file without transformations.
242 241
243#### Image Transformations 242#### Image Transformations
@@ -245,23 +244,23 @@ the original media file without transformations.
245##### Resizing 244##### Resizing
246 245
247Upon upload, `servers` may create resized image variants, such as thumbnails, respecting the original aspect ratio. 246Upon upload, `servers` may create resized image variants, such as thumbnails, respecting the original aspect ratio.
248`Clients` may use the `w` query parameter to request an image version with the desired pixel width. 247`clients` may use the `w` query parameter to request an image version with the desired pixel width.
249`Servers` can then serve the variant with the closest width to the parameter value 248`servers` can then serve the variant with the closest width to the parameter value
250or an image variant generated on the fly. 249or an image variant generated on the fly.
251 250
252Example: `https://your-file-server.example/custom-api-path/<sha256-file-hash>.png?w=32` 251Example: `$api_url/<sha256-hash>.png?w=32`
253 252
254## Deletion 253## Deletion
255 254
256`Servers` must make available the route `https://deletion.domain/deletion-path/<sha256-file-hash>(.ext)` (route taken from `https://your-file-server.example/.well-known/nostr/nip96.json` "api_url" field) with `DELETE` method for file deletion. 255`DELETE $api_url/<sha256-hash>(.ext)`
257 256
258Note that the "\<sha256-file-hash\>" part is from the **original** file, **not** from the **transformed** file if the uploaded file went through any server transformation. 257**AUTH required**
259 258
260The extension is optional as the file hash is the only needed file identification. 259Note that the `/<sha256-hash>` part is from the **original** file, **not** from the **transformed** file if the uploaded file went through any server transformation.
261 260
262`Clients` should send a `DELETE` request to the server deletion route in the above format. It must include a NIP-98 `Authorization` header. 261The extension is optional as the file hash is the only needed file identification.
263 262
264The `server` should reject deletes from users other than the original uploader. The `pubkey` encoded on the header value identifies the user. 263The `server` should reject deletes from users other than the original uploader with the appropriate http response code (403 Forbidden).
265 264
266It should be noted that more than one user may have uploaded the same file (with the same hash). In this case, a delete must not really delete the file but just remove the user's `pubkey` from the file owners list (considering the server keeps just one copy of the same file, because multiple uploads of the same file results 265It should be noted that more than one user may have uploaded the same file (with the same hash). In this case, a delete must not really delete the file but just remove the user's `pubkey` from the file owners list (considering the server keeps just one copy of the same file, because multiple uploads of the same file results
267in the same file hash). 266in the same file hash).
@@ -275,6 +274,35 @@ The successful response is a 200 OK one with just basic JSON fields:
275} 274}
276``` 275```
277 276
277## Listing files
278
279`GET $api_url`
280
281**AUTH required**
282
283Returns a list of files linked to the authenticated users pubkey.
284
285Example Response:
286```js
287[
288 {
289 "id": "<sha256-hash>",
290 "nip94_event": {...},
291 "expires": 1715691139, // unix timestamp
292 "size": 123456,
293 "alt": "a meme that makes you laugh",
294 "caption": "haha funny meme"
295 },
296 ...
297]
298```
299
300`<sha256-hash>` is the **original hash**, ie. `ox`
301
302`nip94_event` is the same as in the upload result.
303
304`alt` / `caption` are optional.
305
278## Selecting a Server 306## Selecting a Server
279 307
280Note: HTTP File Storage Server developers may skip this section. This is meant for client developers. 308Note: HTTP File Storage Server developers may skip this section. This is meant for client developers.