gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitc96709c8c96709c8media-player: skip = false and repeat = 'one' options (jukebox#19)mrec96709c8/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

  • c96709c8media-player: skip = false and repeat = 'one' options (jukebox#19)mre
  • bd4aa808Record VL24 deployment and live verificationmre
  • e5b98ecfRun virtual-list gate before deploymentmre
  • 2e9b4a8evirtual-list: into main, with centerId and scrollTop/onScroll (jukebox#20)mre
  • 69954249Align all plugin sources with Hybriel day38bmre
  • f740f666Re-vendor components to Hybriel set day38bmre
  • 8176eb14Record CS23b deployment and live verificationmre
  • 84e3ddaaRemove blank line before pagination Style closing bracemre
  • a92298ddRecord exact CSS and same-date proof after specificity reviewmre
  • c71a0abePreserve consumer theme specificity with nested tag rulesmre
  • 64a2bc27Document CS23b style formatting proof and review statusmre
  • a72b57cficons: nest Style rules in the language's formmre
  • 927616e2pagination: nest Style rules in the language's formmre
  • aab8ad29code-editor: nest Style rules in the language's formmre
  • ba30727afile-upload: nest Style rules in the language's formmre
  • 42fefa4emodal: nest Style rules in the language's formmre
  • 6fd953e3range-picker: nest Style rules in the language's formmre
  • f661951ddate-picker: nest Style rules in the language's formmre
  • 363577e6media-player: nest Style rules in the language's formmre
  • 0b3fd8c9comments: nest Style rules in the language's formmre