components
All repositories: gitoria
2.3 KB
// lib/uploads.hl — WHERE THE SHOWCASE KEEPS UPLOADED FILES (the site's decision, not the components'): the file upload demo// 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// storage/md-files (served at /md-editor/files/…, see project.hl), only with a known kind of extension (nothing a browser would// run). Both keep a file for one day: every upload first stores, then removes what is older.import { writeFile, mkDir, listDir, remove } from 'hl:fs'import { randomBytes } from 'hl:crypto'import { now } from 'hl:time'static kinds = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'pdf', 'txt', 'md', 'zip']// the file upload demo: one file in, its name and size backstatic storeUpload = (file) => {mkDir('storage/uploads')writeFile('storage/uploads/' + now() + '-' + randomBytes(6) + '.bin', file.bytes)dropOld('storage/uploads')return { name = file.name size = file.size }}// 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 backstatic storeMessage = (text, files) => {mkDir('storage/uploads')let names = ''for (f of files) {writeFile('storage/uploads/' + now() + '-' + randomBytes(6) + '.bin', f.bytes)names = names + (names == '' ? '' : ', ') + f.name + ' (' + f.bytes.length + ' B)'}dropOld('storage/uploads')return { text = text files = names }}// the Markdown editor demo: files in, the address of each back, in orderstatic storeMdFiles = (files) => {mkDir('storage/md-files')urls = []for (f of files) {let stored = now() + '-' + randomBytes(6) + '.' + extensionOf(f.name)writeFile('storage/md-files/' + stored, f.bytes)urls.push('/md-editor/files/' + stored)}dropOld('storage/md-files')return urls}// the stored extension: the file's own when it is a known kind, else 'bin'static extensionOf = (name) => {dot = name.lastIndexOf('.')ext = dot >= 0 ? name.slice(dot + 1).toLowerCase() : ''let known = falsefor (k of kinds) { if (k == ext) { known = true } }return known ? ext : 'bin'}// removes the files of a folder stored more than a day ago (the name starts with the time it was stored)static dropOld = (dir) => {for (e of listDir(dir)) {let stamp = toNumber(e.name.slice(0, e.name.indexOf('-')))if (stamp != null && stamp < now() - 86400000) { remove(e.path) }}}
Branches
- b27b
- mainmain branch
- vl27-resetkey
Latest commits
- 5e090ffdRecord IC28 review evidence and gate resultsmre
- adfe2c4dAdd shuffle, repeat and repeatOne shared iconsmre
- 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