in_old
Clone
https://git.tahinli.com/Tahinli/in_old.git
0 watching0 forksRustGPL-3.01.1 MiB218b775Tahinli2 months ago90 commits
| 1 | //! The chunked-upload protocol. |
| 2 | //! |
| 3 | //! Five owner-only JSON calls, all UTF-8: `POST /api/upload/start` |
| 4 | //! `{folder_id, name, size_bytes}` answers `{id, chunk_size}` with the |
| 5 | //! server-fixed 8 MiB chunk size (`Refusal::QuotaExceeded` when the quota |
| 6 | //! says no — the account quota is the only size limit); `GET /api/upload/{id}` |
| 7 | //! answers the session with the indexes already staged, so an interrupted |
| 8 | //! upload resumes instead of starting over; `PUT /api/upload/{id}/{index}` |
| 9 | //! takes one raw chunk (32 MiB cap, layered in `main.rs`); |
| 10 | //! `POST /api/upload/{id}/finish` assembles the |
| 11 | //! staged chunks, sniffs the mime, moves the bytes to `files/<new file id>` |
| 12 | //! and answers with the created file id; `POST /api/upload/{id}/abort` |
| 13 | //! drops the session and its staged bytes. Quota is enforced at start *and* |
| 14 | //! at finish. The UI picks this path at 8 MiB and over; below that it posts |
| 15 | //! straight to `POST /files`. |
| 16 | //! |
| 17 | //! Answers are `200` JSON throughout — `{ok: ...}` or `{err: <refusal>}` — |
| 18 | //! because the caller is script, not a form: there is no page to redirect |
| 19 | //! back to, and the progress bar reads the refusal straight off the body. |
| 20 | |
| 21 | use StoreError; |
| 22 | use Result; |
| 23 | use Cx; |
| 24 | use Json; |
| 25 | use ; |
| 26 | use ; |
| 27 | |
| 28 | use crate; |
| 29 | |
| 30 | path_param!; |
| 31 | |
| 32 | /// What the script side reads: the value, or the refusal it failed with. |
| 33 | /// Serialises as `{"ok":...}` / `{"err":...}` — a hand-rolled enum because |
| 34 | /// serde's own `Result` would say `Ok`/`Err`, which the UI script does not |
| 35 | /// read. |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | /// A [`StoreError`] in the upload's own words. Another owner's session id |
| 57 | /// answers [`Refusal::NotFound`]: the id space is not theirs to probe. |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | /// The owned, active session the call names, or the refusal its answer |
| 69 | /// carries. Another owner's session is not found rather than forbidden. |
| 70 | async |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | /// Opens an upload session after the quota pre-check: the declared total |
| 97 | /// must fit the remaining quota, or the browser learns before sending a |
| 98 | /// single chunk. |
| 99 | |
| 100 | async |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | /// Stages one raw chunk. The store refuses a wrong-sized or out-of-range |
| 133 | /// piece with [`StoreError::BadChunk`] and an expired session with |
| 134 | /// [`StoreError::UploadExpired`]; this layer only gates the owner. |
| 135 | |
| 136 | async |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | /// The session and the indexes already staged, for a resuming uploader: |
| 172 | /// the client skips every index in `uploaded` and sends only what is |
| 173 | /// missing. An expired session still answers — the chunks it holds are |
| 174 | /// real until the sweep takes them — so the client reads the expiry off |
| 175 | /// `chunk`'s refusal, not here. |
| 176 | |
| 177 | async |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | /// Assembles the staged chunks — rechecking the quota, sniffing the mime |
| 198 | /// off the bytes, attempting the thumbnail — and answers the created file |
| 199 | /// id. The chunks stay staged on refusal, so the browser may retry. |
| 200 | |
| 201 | async |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | /// Aborts a session: the row says `aborted` and the staged chunks go. |
| 215 | /// Aborting twice — or aborting nothing — is not an error. |
| 216 | |
| 217 | async |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 |