gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit0d6863d60d6863d6reports/F3-mdvalue.mdmre0d6863d6/lib/uploads.hl

2.3 KB

  1. // lib/uploads.hl — WHERE THE SHOWCASE KEEPS UPLOADED FILES (the site's decision, not the components'): the file upload demo
  2. // keeps each file in storage/uploads under a random name (nothing is served back; the pick-first demo too); the Markdown editor demo keeps its files in
  3. // storage/md-files (served at /md-editor/files/…, see project.hl), only with a known kind of extension (nothing a browser would
  4. // run). Both keep a file for one day: every upload first stores, then removes what is older.
  5. import { writeFile, mkDir, listDir, remove } from 'hl:fs'
  6. import { randomBytes } from 'hl:crypto'
  7. import { now } from 'hl:time'
  8. static kinds = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'pdf', 'txt', 'md', 'zip']
  9. // the file upload demo: one file in, its name and size back
  10. static storeUpload = (file) => {
  11. mkDir('storage/uploads')
  12. writeFile('storage/uploads/' + now() + '-' + randomBytes(6) + '.bin', file.bytes)
  13. dropOld('storage/uploads')
  14. return { name = file.name size = file.size }
  15. }
  16. // the pick-first demo (files upload in pick mode): a message's text and its files in; the text and "name (size)" of each file back
  17. static storeMessage = (text, files) => {
  18. mkDir('storage/uploads')
  19. let names = ''
  20. for (f of files) {
  21. writeFile('storage/uploads/' + now() + '-' + randomBytes(6) + '.bin', f.bytes)
  22. names = names + (names == '' ? '' : ', ') + f.name + ' (' + f.bytes.length + ' B)'
  23. }
  24. dropOld('storage/uploads')
  25. return { text = text files = names }
  26. }
  27. // the Markdown editor demo: files in, the address of each back, in order
  28. static storeMdFiles = (files) => {
  29. mkDir('storage/md-files')
  30. urls = []
  31. for (f of files) {
  32. let stored = now() + '-' + randomBytes(6) + '.' + extensionOf(f.name)
  33. writeFile('storage/md-files/' + stored, f.bytes)
  34. urls.push('/md-editor/files/' + stored)
  35. }
  36. dropOld('storage/md-files')
  37. return urls
  38. }
  39. // the stored extension: the file's own when it is a known kind, else 'bin'
  40. static extensionOf = (name) => {
  41. dot = name.lastIndexOf('.')
  42. ext = dot >= 0 ? name.slice(dot + 1).toLowerCase() : ''
  43. let known = false
  44. for (k of kinds) { if (k == ext) { known = true } }
  45. return known ? ext : 'bin'
  46. }
  47. // removes the files of a folder stored more than a day ago (the name starts with the time it was stored)
  48. static dropOld = (dir) => {
  49. for (e of listDir(dir)) {
  50. let stamp = toNumber(e.name.slice(0, e.name.indexOf('-')))
  51. if (stamp != null && stamp < now() - 86400000) { remove(e.path) }
  52. }
  53. }

Branches

Latest commits

  • 0d6863d6reports/F3-mdvalue.mdmre
  • d58ed98bMdEditor: an older text of the editor handed back is not an outside change (kept surface, written back); gate for itmre
  • c2d0de59MdEditor: show a value that changes from outside (data-value observer), demo button and gatemre
  • c4272fa3re-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • 2e22618etests: cdp-blank/md-bg/md-keys-run Chrome range and base port from env (MDK_CHROME, MDK_PORT), defaults unchangedmre
  • a5d2384bre-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • 52902d23docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 022b943fdeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • d17f8543mission 012: re-vendor Hybriel master f9614007 (plugins), fixes comments push redraw; gate 480/0mre
  • 5f95de05mission 012: re-vendor Hybriel ce6f2e0a (plugins), report: gate 478/2 on comments push redrawmre
  • 3794c591mission 011: report, LOG, STATUS, md-keys-run widths, cdp-blank helpermre
  • 77a93c16mission 011: gate hook for md-keys (480/0), flatten() runs one at a time (removeChild race), surface also built on first focus / pointer-down (background tab), md-bg test, README keysmre
  • 3c906718mission 011: md-editor key tests (real keys) and fixes: nested p/ul flatten, bare br, link change, Ctrl+E, code block exit (WIP)mre
  • 1442e583mission 010: report, LOG, STATUSmre
  • 6f27ad31md-editor: phone toolbar row contained (inline-size) so it never widens a grid parentmre
  • d702d76dmd-editor look: root class md-empty (a host .empty rule hit it), rows = surface height, one phone toolbar row with 44 px targets; comment form rows 3; gate 360/0mre
  • 15be2ac0comments: attach (FilesUpload pick), onPost(text, files), files per comment; md-editor files=off; README, gate fixmre
  • 1f8d79d6comments (mission 009 WIP, tickets#27): attachments — attach="true" puts the form in FilesUpload pick mode, onPost(text, files), files per comment (thumbnails open full size, file rows, no url = no link); MdEditor files="off"; demo with attachments; gate m009 checks (WIP: 356/1)mre
  • 13389d67mission 008: report, LOG, STATUSmre
  • ab47e4a2files-upload (mission 008, #18 for hycon#46): pick-first-send-later mode — host-owned list (files + onChange), slot for the host's input area, Ctrl+V paste (both modes), thumbnails, name+size rows, ✕ remove, maxSize per file, Camera on touch screens, 44 px targets; demo /files-upload/pick; gate 343/0 (19 new, 18 fail on the old code)mre