dsh-design-studio 1.5.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +6 -5
- package/README.md +7 -6
- package/THIRD_PARTY_NOTICES.md +108 -0
- package/assets/art/README.md +102 -0
- package/assets/art/brand-mark.webp +0 -0
- package/assets/art/check-clean.webp +0 -0
- package/assets/art/coach-markup.webp +0 -0
- package/assets/art/drop-file.webp +0 -0
- package/assets/art/empty-components.webp +0 -0
- package/assets/art/empty-data.webp +0 -0
- package/assets/art/empty-error.webp +0 -0
- package/assets/art/empty-frames.webp +0 -0
- package/assets/art/empty-history.webp +0 -0
- package/assets/art/empty-market.webp +0 -0
- package/assets/art/empty-notes.webp +0 -0
- package/assets/art/empty-search.webp +0 -0
- package/assets/art/empty-tokens.webp +0 -0
- package/assets/art/empty-works.webp +0 -0
- package/assets/art/guide-icon.webp +0 -0
- package/assets/art/kind-app.webp +0 -0
- package/assets/art/kind-desktop.webp +0 -0
- package/assets/art/kind-diagram.webp +0 -0
- package/assets/art/kind-dshplugin.webp +0 -0
- package/assets/art/kind-email.webp +0 -0
- package/assets/art/kind-embedded.webp +0 -0
- package/assets/art/kind-framed.webp +0 -0
- package/assets/art/kind-miniprogram.webp +0 -0
- package/assets/art/kind-poster.webp +0 -0
- package/assets/art/kind-slides.webp +0 -0
- package/assets/art/kind-tablet.webp +0 -0
- package/assets/art/kind-web.webp +0 -0
- package/assets/art/placeholder-inspire-img.webp +0 -0
- package/assets/art/wait-creating.webp +0 -0
- package/assets/demo.gif +0 -0
- package/assets/hero-sm.jpg +0 -0
- package/assets/motion/ai-capsule.html +124 -0
- package/assets/motion/ai-glow-input.html +112 -0
- package/assets/motion/balance-odometer.html +112 -0
- package/assets/motion/bottom-sheet.html +149 -0
- package/assets/motion/brightness-pill.html +114 -0
- package/assets/motion/comet-ring.html +95 -0
- package/assets/motion/copy-morph.html +116 -0
- package/assets/motion/cube-grid.html +103 -0
- package/assets/motion/day-night.html +120 -0
- package/assets/motion/dial-knob.html +130 -0
- package/assets/motion/dock-magnify.html +118 -0
- package/assets/motion/download-ring.html +135 -0
- package/assets/motion/expand-card.html +137 -0
- package/assets/motion/flip-card.html +100 -0
- package/assets/motion/hold-pay.html +137 -0
- package/assets/motion/holo-card.html +130 -0
- package/assets/motion/keycap.html +92 -0
- package/assets/motion/like-jelly.html +88 -0
- package/assets/motion/liquid-progress.html +113 -0
- package/assets/motion/liquid-segment.html +127 -0
- package/assets/motion/live-capsule.html +144 -0
- package/assets/motion/marker-highlight.html +94 -0
- package/assets/motion/marquee-logos.html +110 -0
- package/assets/motion/otp-code.html +111 -0
- package/assets/motion/password-eye.html +136 -0
- package/assets/motion/ruler-slider.html +140 -0
- package/assets/motion/search-expand.html +81 -0
- package/assets/motion/send-fly.html +121 -0
- package/assets/motion/skeleton-shine.html +124 -0
- package/assets/motion/star-rate.html +98 -0
- package/assets/motion/success-check.html +114 -0
- package/assets/motion/suggest-chips.html +134 -0
- package/assets/motion/swipe-row.html +126 -0
- package/assets/motion/tabbar-bounce.html +92 -0
- package/assets/motion/tactile-controls.html +89 -0
- package/assets/motion/text-scramble.html +173 -0
- package/assets/motion/toast-stack.html +115 -0
- package/assets/motion/token-stream.html +106 -0
- package/assets/motion/voice-orb.html +144 -0
- package/assets/showcase/alley-noodles-1.webp +0 -0
- package/assets/showcase/alley-noodles-2.webp +0 -0
- package/assets/showcase/alley-noodles-3.webp +0 -0
- package/assets/showcase/autumn-sale-1.webp +0 -0
- package/assets/showcase/bold-folio-1.webp +0 -0
- package/assets/showcase/bold-folio-2.webp +0 -0
- package/assets/showcase/book-club-1.webp +0 -0
- package/assets/showcase/book-club-2.webp +0 -0
- package/assets/showcase/book-club-3.webp +0 -0
- package/assets/showcase/flower-order-1.webp +0 -0
- package/assets/showcase/little-painter-1.webp +0 -0
- package/assets/showcase/little-painter-2.webp +0 -0
- package/assets/showcase/moon-fair-1.webp +0 -0
- package/assets/showcase/nail-booking-1.webp +0 -0
- package/assets/showcase/nail-booking-2.webp +0 -0
- package/assets/showcase/nail-booking-3.webp +0 -0
- package/assets/showcase/night-run-1.webp +0 -0
- package/assets/showcase/paw-diary-1.webp +0 -0
- package/assets/showcase/paw-diary-2.webp +0 -0
- package/assets/showcase/paw-diary-3.webp +0 -0
- package/assets/showcase/pixel-planet-1.webp +0 -0
- package/assets/showcase/pixel-planet-2.webp +0 -0
- package/assets/showcase/pixel-planet-3.webp +0 -0
- package/assets/showcase/pocket-ledger-1.webp +0 -0
- package/assets/showcase/pocket-ledger-2.webp +0 -0
- package/assets/showcase/shiftloom-1.webp +0 -0
- package/assets/showcase/shiftloom-2.webp +0 -0
- package/assets/showcase/slow-breath-1.webp +0 -0
- package/assets/showcase/slow-breath-2.webp +0 -0
- package/assets/showcase/slow-breath-3.webp +0 -0
- package/assets/showcase/slow-trip-1.webp +0 -0
- package/assets/showcase/slow-trip-2.webp +0 -0
- package/assets/showcase/slow-trip-3.webp +0 -0
- package/assets/showcase/slow-weekly-1.webp +0 -0
- package/assets/showcase/stay-confirm-1.webp +0 -0
- package/assets/showcase/stove-recipes-1.webp +0 -0
- package/assets/showcase/stove-recipes-2.webp +0 -0
- package/assets/showcase/strobewatch-1.webp +0 -0
- package/assets/showcase/strobewatch-2.webp +0 -0
- package/assets/showcase/tapeworks-1.webp +0 -0
- package/assets/showcase/tapeworks-2.webp +0 -0
- package/assets/showcase/tea-brand-pitch-1.webp +0 -0
- package/assets/showcase/tea-brand-pitch-2.webp +0 -0
- package/assets/showcase/tea-brand-pitch-3.webp +0 -0
- package/assets/showcase/time-bookshop-1.webp +0 -0
- package/assets/showcase/time-bookshop-2.webp +0 -0
- package/assets/showcase/water-journey-1.webp +0 -0
- package/assets/showcase/water-journey-2.webp +0 -0
- package/assets/showcase/water-journey-3.webp +0 -0
- package/assets/showcase/weekend-go-1.webp +0 -0
- package/assets/showcase/weekend-go-2.webp +0 -0
- package/assets/showcase/weekend-go-3.webp +0 -0
- package/assets/showcase/winter-soup-1.webp +0 -0
- package/assets/showcase/year-review-1.webp +0 -0
- package/assets/showcase/year-review-2.webp +0 -0
- package/assets/showcase/year-review-3.webp +0 -0
- package/lib/browser.js +3 -1
- package/lib/bundle.js +8 -3
- package/lib/canvthumb.js +9 -6
- package/lib/client.js +7428 -2624
- package/lib/components.js +4 -2
- package/lib/core.js +762 -132
- package/lib/design-page-settings.js +48 -0
- package/lib/design-scope.js +36 -0
- package/lib/design-transactions.js +259 -0
- package/lib/drop-folder.js +25 -0
- package/lib/edits.js +125 -8
- package/lib/embedded/lvgl_shot.c +133 -0
- package/lib/embedded/qml_shot.py +66 -0
- package/lib/embedded-shot.js +400 -0
- package/lib/embedded-trust.js +76 -0
- package/lib/fonts.js +30 -21
- package/lib/frames.js +23 -23
- package/lib/gallery/css.js +9 -2
- package/lib/gallery/market-routes.js +23 -7
- package/lib/gallery/pack.js +25 -8
- package/lib/gallery/policy.js +6 -4
- package/lib/gallery/routes.js +255 -83
- package/lib/gallery/submit.js +285 -57
- package/lib/gallery/use.js +13 -4
- package/lib/handoff-embedded.js +1182 -0
- package/lib/handoff.js +212 -71
- package/lib/history.js +393 -43
- package/lib/htmlpack.js +34 -13
- package/lib/impl.js +3 -0
- package/lib/index.js +661 -136
- package/lib/inspire.js +73 -26
- package/lib/kinds.js +11 -2
- package/lib/look.js +45 -25
- package/lib/mock.js +41 -4
- package/lib/motion.js +90 -0
- package/lib/page-settings.js +103 -0
- package/lib/picker.js +348 -70
- package/lib/rules.js +85 -0
- package/lib/serve.js +154 -15
- package/lib/showcase.json +22 -0
- package/lib/styles.js +16 -7
- package/lib/system.js +19 -8
- package/lib/tool.js +2 -1
- package/lib/tweaks.js +146 -9
- package/lib/works.js +151 -19
- package/locale/auto/en.json +1 -1
- package/locale/auto/zh.json +1 -1
- package/locale/en.json +1 -1
- package/locale/offline/en.json +1 -1
- package/locale/offline/zh.json +2 -2
- package/locale/zh.json +1 -1
- package/package.json +3 -2
- package/skills/ui-design/SKILL.md +56 -46
- package/skills/ui-design/frames/dsh/frame.json +1 -1
- package/skills/ui-design/references/app.md +29 -2
- package/skills/ui-design/references/components.md +1 -1
- package/skills/ui-design/references/desktop.md +1 -1
- package/skills/ui-design/references/dshplugin.md +12 -12
- package/skills/ui-design/references/embedded.md +40 -0
- package/skills/ui-design/references/frames.md +20 -20
- package/skills/ui-design/references/miniprogram.md +2 -2
- package/skills/ui-design/references/mock.md +16 -0
- package/skills/ui-design/references/poster.md +2 -1
- package/skills/ui-design/references/tweaks.md +17 -0
- package/skills/ui-design/tokens.css +19 -5
package/lib/gallery/routes.js
CHANGED
|
@@ -7,10 +7,10 @@
|
|
|
7
7
|
* POST pack { dir, license } 打成 zip:先规范化,再用和机器人同一份规则查;过了存到「下载」
|
|
8
8
|
* POST usezip { session, name, data } 拖进来的 zip →「用这个开始」:查、落到新文件夹、登记、打开
|
|
9
9
|
* GET inspire 灵感(参考图、网址、我的风格)
|
|
10
|
-
* POST inspire { op, … } addImage / addUrl / addStyle / addMarket / remove
|
|
10
|
+
* POST inspire { op, … } addImage / addUrl / addStyle / addMarket / addComponent / remove
|
|
11
11
|
* GET inspimg ?id= 灵感里的图片
|
|
12
12
|
* GET/POST ghauth { op } 一键投稿:连 GitHub(设备授权)、断开
|
|
13
|
-
* POST submit { path }
|
|
13
|
+
* POST submit { path, op? } 一键投稿、核对投稿单、查看进度或撤回尚未成为投稿单的上传
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
import crypto from 'node:crypto'
|
|
@@ -19,14 +19,17 @@ import os from 'node:os'
|
|
|
19
19
|
import path from 'node:path'
|
|
20
20
|
import { execFile } from 'node:child_process'
|
|
21
21
|
import { listScreens, allScreens } from '../core.js'
|
|
22
|
-
import {
|
|
22
|
+
import { matchesTarget } from '../design-scope.js'
|
|
23
|
+
import { fileDependencies } from '../history.js'
|
|
24
|
+
import { parseMeta, metaOfFile, sizeOf, KIND_SIZE, prepare } from '../look.js'
|
|
23
25
|
import { packDesign, zipOf, splitTitle } from './pack.js'
|
|
24
26
|
import { PERSONAL_CODES } from './policy.js'
|
|
25
27
|
import { pageUsesComponents, dirReader } from '../comp-io.js'
|
|
26
28
|
import { readZip, ZipError } from './zip.js'
|
|
27
29
|
import { renderPack } from './render.js'
|
|
28
30
|
import { preparePack, installPack, baseFor, slugOf, UseError } from './use.js'
|
|
29
|
-
import {
|
|
31
|
+
import { LIBRARY_DIR } from '../works.js'
|
|
32
|
+
import { createGitHub, GhError, issueNumber, packPrint, submissionFiles, ZIP_MAX } from './submit.js'
|
|
30
33
|
|
|
31
34
|
const THUMB_DIR = path.join(os.homedir(), '.dsh', 'cache', 'dsh-design-studio', 'works', 'thumbs')
|
|
32
35
|
const MAX_ZIP_BODY = 12 * 1024 * 1024 // base64 的 8MB zip
|
|
@@ -60,10 +63,10 @@ const TOKEN_RE = /^[0-9a-f]{32}$/
|
|
|
60
63
|
const DESIGN_LANGS = ['zh', 'en', 'both'] // design.json 的 lang:设计里的文字是哪种语言
|
|
61
64
|
/** 分享记号 → 目录里的指纹(sha256 前 32 位)和给人看的短号 */
|
|
62
65
|
export const shareHashOf = (token) => crypto.createHash('sha256').update(String(token)).digest('hex').slice(0, 32)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (
|
|
66
|
-
const hash = shareHashOf(
|
|
66
|
+
const manifestToken = (manifest) => typeof manifest?.shareToken === 'string' && TOKEN_RE.test(manifest.shareToken) ? manifest.shareToken : ''
|
|
67
|
+
function shareOf(token) {
|
|
68
|
+
if (!token) return null
|
|
69
|
+
const hash = shareHashOf(token)
|
|
67
70
|
return { hash, ref: hash.slice(0, 12) }
|
|
68
71
|
}
|
|
69
72
|
/** 投稿表单的「短名」:标题或文件夹名能拼成 a-z0-9- 就预填,不然留空让机器人生成 */
|
|
@@ -85,8 +88,8 @@ const hardMsg = (x, en) => {
|
|
|
85
88
|
}
|
|
86
89
|
|
|
87
90
|
/** 一条作品记录 → 右侧栏卡片要的样子(目录不在了 exists: false) */
|
|
88
|
-
//
|
|
89
|
-
const THUMB_V = '|
|
|
91
|
+
// 缩略图的截法改了就换一个:签名带上它,旧图自然作废
|
|
92
|
+
const THUMB_V = '|t3'
|
|
90
93
|
|
|
91
94
|
/** 设计目录 components/ 下各组件文件的指纹(#30;没有组件给 '',老封面的签名不变) */
|
|
92
95
|
function compSig(dir) {
|
|
@@ -97,13 +100,13 @@ function compSig(dir) {
|
|
|
97
100
|
return s
|
|
98
101
|
}
|
|
99
102
|
|
|
100
|
-
export function describeWork(w) {
|
|
103
|
+
export function describeWork(w, publication) {
|
|
101
104
|
const dir = w.dir
|
|
102
105
|
let exists = false
|
|
103
|
-
try { exists = fs.statSync(dir).isDirectory() } catch { exists = false }
|
|
104
|
-
const base = { id: idOf(dir), dir, name: path.basename(dir), pinned: !!w.pinned, trust: w.trust || '', from: w.from || null, added: w.added || 0, exists, share:
|
|
106
|
+
try { exists = w.file ? fs.lstatSync(w.file).isFile() : fs.statSync(dir).isDirectory() } catch { exists = false }
|
|
107
|
+
const base = { id: w.workId || idOf(dir), workId: w.workId || idOf(dir), designId: w.designId, scope: w.scope || 'directory', target: w.file || dir, dir, file: w.file || '', name: path.basename(w.file || dir), pinned: !!w.pinned, trust: w.trust || '', from: w.from || null, added: w.added || 0, exists, share: ['submitted', 'published'].includes(publication?.state) ? publication.share : null, publication: publication || null }
|
|
105
108
|
if (!exists) return { ...base, title: w.title || path.basename(dir), kind: w.kind || '', screens: w.screens || 0, updated: w.updated || w.seen || 0 }
|
|
106
|
-
const screens = listScreens(dir)
|
|
109
|
+
const screens = w.file ? [path.basename(w.file)] : listScreens(dir)
|
|
107
110
|
let updated = 0
|
|
108
111
|
let sig = ''
|
|
109
112
|
for (const f of screens) {
|
|
@@ -117,16 +120,58 @@ export function describeWork(w) {
|
|
|
117
120
|
const first = screens[0] ? path.join(dir, screens[0]) : ''
|
|
118
121
|
const product = first ? splitTitle(headTitle(first)).product : ''
|
|
119
122
|
const kind = w.kind || (first ? parseMeta(metaOfFile(first)).kind : '') || 'web'
|
|
120
|
-
|
|
123
|
+
if (sig) sig += '|' + kind + '|' + (localManifest(dir)?.size || '') + '|' + (screens.length === 1)
|
|
124
|
+
return { ...base, title: (!w.file && manifestTitle(dir)) || product || w.title || path.basename(w.file || dir), kind, screens: screens.length, first: screens[0] || '', updated: Math.round(updated || w.updated || w.seen || 0), thumb: sig ? crypto.createHash('sha1').update(sig + THUMB_V).digest('hex').slice(0, 10) : '' }
|
|
121
125
|
}
|
|
122
126
|
|
|
123
|
-
export function createLibraryRoutes({ admit, readBody, json, tracker, library, inspiration, studio, open, storeLoad, version = '', exported, log = () => {}, thumbDir = THUMB_DIR, downloads = path.join(os.homedir(), 'Downloads'), sessionCwd = async (id) => tracker.cwdOf(id), render = renderPack, github = createGitHub({ log }), vocab = () => ({ categories: [], tags: [] }) }) {
|
|
127
|
+
export function createLibraryRoutes({ admit, readBody, json, tracker, library, history, inspiration, studio, open, storeLoad, version = '', exported, log = () => {}, thumbDir = THUMB_DIR, downloads = path.join(os.homedir(), 'Downloads'), sessionCwd = async (id) => tracker.cwdOf(id), render = renderPack, github = createGitHub({ log }), vocab = () => ({ categories: [], tags: [] }), catalog = () => null, records = submissionFiles(path.join(library.file ? path.dirname(library.file) : LIBRARY_DIR, 'gallery-shares')) }) {
|
|
124
128
|
const lang = (body) => (body && body.lang === 'en' ? 'en' : 'zh')
|
|
125
|
-
|
|
126
|
-
|
|
129
|
+
const dirKey = (dir) => {
|
|
130
|
+
try { return 'work:' + fs.realpathSync(dir) } catch { return 'work:' + path.resolve(dir) }
|
|
131
|
+
}
|
|
132
|
+
const tokenOf = (dir, manifest = localManifest(dir)) => {
|
|
133
|
+
const work = library.get(dir)
|
|
134
|
+
return manifestToken(manifest) || records.read(dirKey(dir))?.token ||
|
|
135
|
+
work?.aliases?.map(alias => records.read(dirKey(alias))?.token).find(Boolean) || ''
|
|
136
|
+
}
|
|
137
|
+
const shareKey = hash => 'share:' + hash
|
|
138
|
+
const zipKey = file => 'zip:' + file
|
|
139
|
+
const progressOf = s => github.progress?.(s.pack) || records.read('progress:' + s.pack)
|
|
140
|
+
function publicationOf(share, listed) {
|
|
141
|
+
if (!share) return null
|
|
142
|
+
const row = records.read(shareKey(share.hash))
|
|
143
|
+
const entry = listed.get(share.hash)
|
|
144
|
+
const issue = row?.issue || (issueNumber(entry?.submission) ? { number: issueNumber(entry.submission), url: entry.submission } : null)
|
|
145
|
+
const details = { share, issue, issuePath: row?.issuePath || '' }
|
|
146
|
+
if (entry) return { state: 'published', ...details, entryId: entry.id }
|
|
147
|
+
if (issue) return { state: issue.state === 'open' ? 'submitted' : issue.state, ...details }
|
|
148
|
+
if (row?.file) return { state: 'prepared', ...details }
|
|
149
|
+
return null
|
|
150
|
+
}
|
|
151
|
+
const knownIssue = s => records.read('issue:' + s.pack)
|
|
152
|
+
function rememberIssue(s, file, issue) {
|
|
153
|
+
if (!issueNumber(issue.number) || issueNumber(issue.url) !== issue.number) throw new GhError('issueMismatch')
|
|
154
|
+
const key = shareKey(s.hash)
|
|
155
|
+
const row = records.read(key) || {}
|
|
156
|
+
const checked = { ...issue, checkedAt: Date.now() }
|
|
157
|
+
records.write('issue:' + s.pack, checked)
|
|
158
|
+
if (!row.issue || issue.number >= row.issue.number) records.write(key, { ...row, issue: checked, issuePath: file })
|
|
159
|
+
return checked
|
|
160
|
+
}
|
|
161
|
+
function rememberProgress(s, file, progress) {
|
|
162
|
+
if (!progress) return
|
|
163
|
+
records.write('progress:' + s.pack, progress)
|
|
164
|
+
const key = shareKey(s.hash)
|
|
165
|
+
const row = records.read(key) || {}
|
|
166
|
+
const uploads = { ...row.uploads }
|
|
167
|
+
const attempt = s.pack + ':' + progress.login.toLowerCase()
|
|
168
|
+
if (progress.canWithdraw) uploads[attempt] = { path: file, pack: s.pack, progress }
|
|
169
|
+
else delete uploads[attempt]
|
|
170
|
+
records.write(key, { ...row, uploads })
|
|
171
|
+
}
|
|
127
172
|
|
|
128
173
|
// 作品库里登记的、目录在哪(id → 记录):只认列得出来的,别让请求指到别的目录
|
|
129
|
-
const byId = (id) => library.list().find(
|
|
174
|
+
const byId = (id) => library.list().find(w => (w.workId || idOf(w.dir)) === id) || null
|
|
130
175
|
const byDir = (dir) => {
|
|
131
176
|
const w = library.get(String(dir || ''))
|
|
132
177
|
return w && !w.hidden ? w : null
|
|
@@ -140,22 +185,32 @@ export function createLibraryRoutes({ admit, readBody, json, tracker, library, i
|
|
|
140
185
|
if (!admit(req, res)) return
|
|
141
186
|
if (req.method === 'GET') {
|
|
142
187
|
synced()
|
|
143
|
-
const
|
|
188
|
+
const market = catalog()
|
|
189
|
+
const listed = new Map([...(market?.designs || []), ...(market?.systems || [])].filter(e => e.shareHash).map(e => [e.shareHash, e]))
|
|
190
|
+
const all = library.list()
|
|
191
|
+
const removed = all.filter(w => w.hidden && w.kind !== 'system').map(({ dir, file, title, workId }) => ({ dir, file, title, id: workId, workId }))
|
|
192
|
+
const list = all.filter(w => !w.hidden).map(w => describeWork(w, w.file ? null : publicationOf(shareOf(tokenOf(w.dir)), listed)))
|
|
144
193
|
list.sort((a, b) => (b.pinned - a.pinned) || (b.exists - a.exists) || (b.updated - a.updated))
|
|
145
|
-
return json(res, 200, { works: list.slice(0, 500) })
|
|
194
|
+
return json(res, 200, { works: list.slice(0, 500), removed })
|
|
146
195
|
}
|
|
147
196
|
if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
|
|
148
197
|
let body
|
|
149
198
|
try { body = await readBody(req) } catch (e) { return json(res, 400, { error: e.message }) }
|
|
150
|
-
const w =
|
|
151
|
-
|
|
199
|
+
const w = body.workId || body.id ? byId(body.workId || body.id)
|
|
200
|
+
: body.op === 'restore' ? library.get(String(body.dir || '')) : byDir(body.dir)
|
|
201
|
+
if (!w || w.hidden && body.op !== 'restore') return json(res, 404, { error: 'not in library' })
|
|
202
|
+
const location = w.file || w.dir
|
|
152
203
|
switch (body.op) {
|
|
153
204
|
case 'pin':
|
|
154
205
|
case 'unpin':
|
|
155
|
-
library.put(
|
|
206
|
+
library.put(location, { pinned: body.op === 'pin' ? true : undefined })
|
|
207
|
+
break
|
|
208
|
+
case 'restore':
|
|
209
|
+
if (w.kind === 'system') return json(res, 400, { error: 'not a work' })
|
|
210
|
+
library.put(location, { hidden: undefined, hiddenAt: undefined })
|
|
156
211
|
break
|
|
157
212
|
case 'hide':
|
|
158
|
-
library.hide(
|
|
213
|
+
library.hide(location)
|
|
159
214
|
break
|
|
160
215
|
case 'reveal': {
|
|
161
216
|
if (!fs.existsSync(w.dir)) return json(res, 404, { error: 'gone' })
|
|
@@ -164,12 +219,34 @@ export function createLibraryRoutes({ admit, readBody, json, tracker, library, i
|
|
|
164
219
|
break
|
|
165
220
|
}
|
|
166
221
|
case 'relocate': {
|
|
167
|
-
|
|
222
|
+
if (typeof body.to !== 'string' || !body.to.trim()) return json(res, 400, { error: 'noScreens' })
|
|
223
|
+
let to = path.resolve(body.to)
|
|
224
|
+
if (w.file) {
|
|
225
|
+
try {
|
|
226
|
+
if (fs.statSync(to).isDirectory()) {
|
|
227
|
+
const files = listScreens(to)
|
|
228
|
+
const name = path.basename(w.file)
|
|
229
|
+
const selected = files.includes(name) ? name : files.length === 1 ? files[0] : ''
|
|
230
|
+
if (!selected) return json(res, 400, { error: 'noScreens', msg: '请选择要重新定位的 HTML 文件' })
|
|
231
|
+
to = path.join(to, selected)
|
|
232
|
+
}
|
|
233
|
+
} catch { /* regular path validation below */ }
|
|
234
|
+
}
|
|
168
235
|
let ok = false
|
|
169
|
-
try {
|
|
236
|
+
try {
|
|
237
|
+
const stat = fs.statSync(to)
|
|
238
|
+
ok = w.file ? fs.lstatSync(to).isFile() && /\.html?$/i.test(to) : stat.isDirectory() && listScreens(to).length > 0
|
|
239
|
+
} catch { /* show the selected destination error */ }
|
|
170
240
|
if (!ok) return json(res, 400, { error: 'noScreens' })
|
|
171
|
-
|
|
172
|
-
|
|
241
|
+
try {
|
|
242
|
+
const moved = library.move(location, to)
|
|
243
|
+
if (!moved) return json(res, 404, { error: 'not in library' })
|
|
244
|
+
await tracker.relocate?.(moved, location)
|
|
245
|
+
return json(res, 200, { ok: true, work: describeWork(moved), ...(body.session ? { state: tracker.snapshot(String(body.session)) } : {}) })
|
|
246
|
+
} catch (e) {
|
|
247
|
+
log(e)
|
|
248
|
+
return json(res, e.code === 'workDestinationConflict' ? 409 : 400, { error: e.code || 'backup', msg: e.message })
|
|
249
|
+
}
|
|
173
250
|
}
|
|
174
251
|
default:
|
|
175
252
|
return json(res, 400, { error: 'bad op' })
|
|
@@ -180,19 +257,21 @@ export function createLibraryRoutes({ admit, readBody, json, tracker, library, i
|
|
|
180
257
|
// 缩略图一次只截一张(无头浏览器同时开的页面有限,别挤掉体检)
|
|
181
258
|
let chain = Promise.resolve()
|
|
182
259
|
const making = new Map()
|
|
183
|
-
function makeThumb(w, d, file) {
|
|
260
|
+
function makeThumb(w, d, file, offline = library.strictRoot(w.dir)) {
|
|
184
261
|
if (making.has(file)) return making.get(file)
|
|
185
262
|
const job = chain.then(async () => {
|
|
186
263
|
const browser = studio.browser
|
|
187
264
|
if (!browser) throw new Error('no browser')
|
|
188
265
|
const { origin } = await open({ kind: 'file', root: w.dir, file: path.join(w.dir, d.first) })
|
|
189
|
-
const
|
|
266
|
+
const declared = parseMeta(metaOfFile(path.join(w.dir, d.first))).size
|
|
267
|
+
const [vw, vh] = sizeOf(declared, d.kind) || sizeOf(localManifest(w.dir)?.size, d.kind) || KIND_SIZE[d.kind] || KIND_SIZE.web
|
|
190
268
|
const scale = Math.min(1, 480 / vw)
|
|
191
|
-
|
|
192
|
-
const tall = vh > vw * 1.15
|
|
193
|
-
const jpg = await browser.withPage({ url: `${origin}/${d.first.split('/').map(encodeURIComponent).join('/')}`, w: vw, h: tall ? vh : Math.min(vh, Math.round(vw * 1.25)), scale, offline: library.strictRoot(w.dir) }, async (page) => {
|
|
269
|
+
const jpg = await browser.withPage({ url: `${origin}/${d.first.split('/').map(encodeURIComponent).join('/')}`, w: vw, h: vh, scale, offline }, async (page) => {
|
|
194
270
|
await prepare(page, false)
|
|
195
|
-
return page.shot({ quality: 70 })
|
|
271
|
+
if (d.screens !== 1) return page.shot({ quality: 70 })
|
|
272
|
+
// 保持页面原来的布局视口,按整张文档取图。长页按比例降低输出分辨率。
|
|
273
|
+
const [width, height] = await page.eval('[Math.max(innerWidth, document.documentElement.scrollWidth, document.body.scrollWidth), Math.max(innerHeight, document.documentElement.scrollHeight, document.body.scrollHeight)]')
|
|
274
|
+
return page.shot({ quality: 70, clip: { x: 0, y: 0, width, height, scale: Math.min(1, 1920 / (Math.max(width, height) * scale)) } })
|
|
196
275
|
})
|
|
197
276
|
fs.mkdirSync(thumbDir, { recursive: true })
|
|
198
277
|
fs.writeFileSync(file + '.tmp', jpg)
|
|
@@ -205,6 +284,14 @@ export function createLibraryRoutes({ admit, readBody, json, tracker, library, i
|
|
|
205
284
|
return job
|
|
206
285
|
}
|
|
207
286
|
|
|
287
|
+
async function renderMarketThumb(staged, first) {
|
|
288
|
+
const { entry, dir } = staged
|
|
289
|
+
const sig = crypto.createHash('sha256').update([entry.digest, entry.rev, first, THUMB_V].join('|')).digest('hex')
|
|
290
|
+
const file = path.join(thumbDir, 'market-' + sig + '.jpg')
|
|
291
|
+
if (!fs.existsSync(file)) await makeThumb({ dir }, { first, kind: entry.kind, screens: entry.screens.length }, file, true)
|
|
292
|
+
return fs.readFileSync(file)
|
|
293
|
+
}
|
|
294
|
+
|
|
208
295
|
async function workthumb(req, res) {
|
|
209
296
|
if (!admit(req, res)) return
|
|
210
297
|
const id = new URL(req.url, 'http://x').searchParams.get('id') || ''
|
|
@@ -216,7 +303,7 @@ export function createLibraryRoutes({ admit, readBody, json, tracker, library, i
|
|
|
216
303
|
// 首页卡片摆前三屏:第二、三屏各截一张,按那屏自己的修改时间缓存(文件名 <id>~<i>-<签名>,和第一屏的分开清)
|
|
217
304
|
let pre = d.id + '-'
|
|
218
305
|
if (i > 0) {
|
|
219
|
-
const f = listScreens(w.dir)[i]
|
|
306
|
+
const f = w.file ? '' : listScreens(w.dir)[i]
|
|
220
307
|
if (!f) return json(res, 404, { error: 'no screens' })
|
|
221
308
|
let sig = ''
|
|
222
309
|
try { const st = fs.statSync(path.join(w.dir, f)); sig = `${f}:${Math.round(st.mtimeMs)}:${st.size}` + compSig(w.dir) } catch { return json(res, 404, { error: 'no screens' }) }
|
|
@@ -240,11 +327,11 @@ export function createLibraryRoutes({ admit, readBody, json, tracker, library, i
|
|
|
240
327
|
|
|
241
328
|
// 分类、标签只收词表里有的(机器人按同一份词表查,不在里面会被拒);没传就沿用 design.json 里原来的
|
|
242
329
|
const vocabNow = () => { try { const v = vocab() || {}; return { categories: Array.isArray(v.categories) ? v.categories : [], tags: Array.isArray(v.tags) ? v.tags : [] } } catch { return { categories: [], tags: [] } } }
|
|
243
|
-
function shareMeta(body) {
|
|
330
|
+
function shareMeta(body, preferences) {
|
|
244
331
|
const v = vocabNow()
|
|
245
332
|
// 词表拿不到(没联网也没种子):这两栏表单里不显示,design.json 里原来的留着
|
|
246
|
-
const ids = (k, max) => (Array.isArray(body[k]) && v[k].length ? [...new Set(body[k].filter((x) => typeof x === 'string' && v[k].some((y) => y.id === x)))].slice(0, max) :
|
|
247
|
-
return { categories: ids('categories', 2), tags: ids('tags', 8), designLang: DESIGN_LANGS.includes(body.designLang) ? body.designLang :
|
|
333
|
+
const ids = (k, max) => (Array.isArray(body[k]) && v[k].length ? [...new Set(body[k].filter((x) => typeof x === 'string' && v[k].some((y) => y.id === x)))].slice(0, max) : preferences?.[k])
|
|
334
|
+
return { categories: ids('categories', 2), tags: ids('tags', 8), designLang: DESIGN_LANGS.includes(body.designLang) ? body.designLang : preferences?.lang }
|
|
248
335
|
}
|
|
249
336
|
|
|
250
337
|
async function pack(req, res) {
|
|
@@ -253,32 +340,73 @@ export function createLibraryRoutes({ admit, readBody, json, tracker, library, i
|
|
|
253
340
|
let body
|
|
254
341
|
try { body = await readBody(req) } catch (e) { return json(res, 400, { error: e.message }) }
|
|
255
342
|
// 只打包作品库里的、或者某个会话正在预览的设计目录
|
|
256
|
-
const
|
|
257
|
-
const
|
|
258
|
-
const
|
|
259
|
-
|
|
260
|
-
const
|
|
343
|
+
const snap = body.session ? await (tracker.readySnapshot?.(String(body.session)) || tracker.snapshot(String(body.session))) : null
|
|
344
|
+
const targetKey = body.targetKey || body.target
|
|
345
|
+
const versionNumber = Number(body.n || body.v) || 0
|
|
346
|
+
const registered = body.workId ? byId(body.workId) : body.dir ? byDir(body.dir) : null
|
|
347
|
+
const live = snap?.target?.kind === 'file' ? snap.target : null
|
|
348
|
+
const matchingActive = live && (!body.dir || path.resolve(body.dir) === live.root || path.resolve(body.dir) === live.file)
|
|
349
|
+
&& (!body.workId || registered?.designId === live.designId)
|
|
350
|
+
if (targetKey && (!matchingActive || !matchesTarget(live, targetKey))) return json(res, 409, { error: 'moved' })
|
|
351
|
+
if (versionNumber && (!targetKey || !body.at || !matchingActive || body.share || !history)) return json(res, 400, { error: 'scope' })
|
|
352
|
+
const work = matchingActive ? null : registered
|
|
353
|
+
if (!matchingActive && (!work || work.hidden)) return json(res, 404, { error: 'not a design here' })
|
|
354
|
+
const dir = matchingActive ? live.root : work.dir
|
|
355
|
+
const single = matchingActive ? live.scope === 'file' : !!work.file
|
|
356
|
+
let versionRow, archived
|
|
357
|
+
if (versionNumber) {
|
|
358
|
+
const version = history.find(live, versionNumber, body.at)
|
|
359
|
+
if (!version) return json(res, 404, { error: 'gone' })
|
|
360
|
+
try { archived = history.contents(live, version) } catch (e) { return json(res, 400, { error: e.code || 'backup', msg: e.message }) }
|
|
361
|
+
versionRow = history.list(live).find(row => row.n === versionNumber)
|
|
362
|
+
}
|
|
363
|
+
const screens = versionRow?.screens || (matchingActive ? tracker.scopeFor?.(live).pages || allScreens(dir) : single ? [path.basename(work.file)] : allScreens(dir))
|
|
261
364
|
if (!screens.length) return json(res, 400, { error: 'noScreens' })
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
if (
|
|
265
|
-
const local = localManifest(dir)
|
|
266
|
-
|
|
365
|
+
const rd = archived ? key => archived.get(key) || null : dirReader(dir)
|
|
366
|
+
if (screens.some(key => { try { return pageUsesComponents(rd(key)?.toString('utf8') || '', rd) } catch { return false } })) return json(res, 400, { error: 'compShare' })
|
|
367
|
+
if (single && body.share) return json(res, 400, { error: 'scope' })
|
|
368
|
+
const local = versionRow || single ? null : localManifest(dir)
|
|
369
|
+
const retained = versionRow || single ? '' : tokenOf(dir, local)
|
|
370
|
+
const claim = shareOf(retained)
|
|
371
|
+
const saved = claim ? records.read(shareKey(claim.hash)) : null
|
|
372
|
+
const preferences = saved?.preferences
|
|
373
|
+
// 分享向导的上次参数留在作品之外,原始设计清单只供读取。
|
|
267
374
|
if (body.info === true) {
|
|
268
375
|
const pick = (v) => (v && typeof v === 'object' ? String(v[lang(body)] || v.zh || v.en || '') : '')
|
|
269
376
|
const v = vocabNow()
|
|
270
|
-
const
|
|
271
|
-
|
|
377
|
+
const meta = preferences || local || {}
|
|
378
|
+
const known = (k, max) => (Array.isArray(meta[k]) ? meta[k].filter((x) => v[k].some((y) => y.id === x)).slice(0, max) : [])
|
|
379
|
+
const prepared = saved?.file ? records.read(zipKey(saved.file)) : null
|
|
380
|
+
return json(res, 200, {
|
|
381
|
+
title: pick(meta.title) || manifestTitle(dir) || path.basename(dir), description: pick(meta.description),
|
|
382
|
+
license: meta.license === 'CC-BY-4.0' ? 'CC-BY-4.0' : 'CC0-1.0', share: claim, screens: screens.length,
|
|
383
|
+
categories: known('categories', 2), tags: known('tags', 8), designLang: DESIGN_LANGS.includes(meta.lang) ? meta.lang : '', vocab: v,
|
|
384
|
+
prepared: prepared ? { ...prepared.result, path: saved.file, share: prepared.share, issue: knownIssue(prepared), progress: progressOf(prepared) } : null,
|
|
385
|
+
uploads: Object.values(saved?.uploads || {}),
|
|
386
|
+
})
|
|
272
387
|
}
|
|
273
|
-
const license = body.license === 'CC-BY-4.0' ? 'CC-BY-4.0' : 'CC0-1.0'
|
|
274
|
-
// 分享到市场:四个确认都要勾;分享记号第一次分享时生成,存在作者自己的 design.json 里,包里只带它的指纹
|
|
275
388
|
const share = body.share === true
|
|
389
|
+
const license = (body.license || (share ? preferences?.license : '')) === 'CC-BY-4.0' ? 'CC-BY-4.0' : 'CC0-1.0'
|
|
390
|
+
// 分享到市场:四个确认都要勾,包里只带认领记号的指纹。
|
|
276
391
|
if (share && !['own', 'brand', 'private', 'rules'].every((k) => body.agree && body.agree[k] === true)) return json(res, 400, { error: 'agree' })
|
|
277
|
-
const token = share ?
|
|
392
|
+
const token = share ? retained || crypto.randomBytes(16).toString('hex') : ''
|
|
393
|
+
if (share && !retained) records.write(dirKey(dir), { token })
|
|
278
394
|
const shareHash = token ? shareHashOf(token) : ''
|
|
279
395
|
let p
|
|
280
396
|
try {
|
|
281
|
-
|
|
397
|
+
const language = lang(body)
|
|
398
|
+
const prefill = (k) => preferences?.[k]?.[language] || preferences?.[k]?.zh || preferences?.[k]?.en || ''
|
|
399
|
+
p = packDesign(dir, {
|
|
400
|
+
screens, version, license, archived, versionRow, single,
|
|
401
|
+
dependencies: single && !archived ? fileDependencies(dir, screens[0]) : null,
|
|
402
|
+
settings: versionRow?.settings || (matchingActive ? snap.pageSettings : undefined),
|
|
403
|
+
author: license === 'CC-BY-4.0' ? String(body.author || (share ? preferences?.author?.name : '') || '') : '',
|
|
404
|
+
mode: share ? 'share' : 'zip', lang: language,
|
|
405
|
+
title: share ? String(body.title || prefill('title')) : '',
|
|
406
|
+
description: share ? String(body.description || prefill('description')) : '',
|
|
407
|
+
...(share ? shareMeta(body, preferences) : {}), shareHash,
|
|
408
|
+
secrets: [...new Set([token, retained, local?.shareToken].filter(Boolean))],
|
|
409
|
+
})
|
|
282
410
|
} catch (e) {
|
|
283
411
|
log(e)
|
|
284
412
|
return json(res, 500, { error: e.message })
|
|
@@ -317,24 +445,24 @@ export function createLibraryRoutes({ admit, readBody, json, tracker, library, i
|
|
|
317
445
|
try {
|
|
318
446
|
fs.mkdirSync(downloads, { recursive: true })
|
|
319
447
|
const t = p.manifest.title
|
|
320
|
-
const stem = slugOf(t.zh || t.en || path.basename(dir), 'design')
|
|
448
|
+
const stem = slugOf(t.zh || t.en || path.basename(dir), 'design') + (versionRow ? '-v' + versionRow.n : '')
|
|
321
449
|
let file = path.join(downloads, `${stem}.zip`)
|
|
322
450
|
for (let i = 2; fs.existsSync(file) && i < 100; i++) file = path.join(downloads, `${stem}-${i}.zip`)
|
|
323
451
|
const zip = zipOf(p)
|
|
324
452
|
fs.writeFileSync(file, zip, { flag: 'wx' })
|
|
325
453
|
exported.add(file)
|
|
326
|
-
if (!share) return json(res, 200, { ...out, path: file })
|
|
327
|
-
|
|
328
|
-
const
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
json(res, 200, { ...out, path: file, share: {
|
|
454
|
+
if (!share) return json(res, 200, { ...out, path: file, ...(versionRow ? { n: versionRow.n, at: versionRow.at } : {}) })
|
|
455
|
+
const pack = packPrint(p.files)
|
|
456
|
+
const sh = {
|
|
457
|
+
...shareOf(token), slug: slugFor(t.en, t.zh, path.basename(dir)), title: t.zh || t.en || '',
|
|
458
|
+
license, pack, manualRef: pack.slice(0, 16),
|
|
459
|
+
}
|
|
460
|
+
records.write(zipKey(file), { hash: shareHash, pack, share: sh, result: out, zipSha: crypto.createHash('sha256').update(zip).digest('hex') })
|
|
461
|
+
const shareId = shareKey(shareHash)
|
|
462
|
+
const previous = records.read(shareId)
|
|
463
|
+
const { title, description, author, categories = [], tags = [], lang: designLang } = p.manifest
|
|
464
|
+
records.write(shareId, { ...previous, file, preferences: { title, description, license, author, categories, tags, lang: designLang } })
|
|
465
|
+
json(res, 200, { ...out, path: file, share: sh, issue: knownIssue({ pack }), progress: progressOf({ pack }) })
|
|
338
466
|
} catch (e) {
|
|
339
467
|
log(e)
|
|
340
468
|
json(res, 500, { error: e.message })
|
|
@@ -369,7 +497,7 @@ export function createLibraryRoutes({ admit, readBody, json, tracker, library, i
|
|
|
369
497
|
from: { zip: [...name].slice(0, 80).join('') || 'design.zip' },
|
|
370
498
|
library,
|
|
371
499
|
})
|
|
372
|
-
await tracker.setTarget(session, { kind: 'file', file: r.first, root: r.dir }, { source: 'user', popup: false, kind: prepared.manifest.kind, size: prepared.manifest.size || '' })
|
|
500
|
+
await tracker.setTarget(session, { kind: 'file', file: r.first, root: r.dir, directory: true }, { source: 'user', popup: false, kind: prepared.manifest.kind, size: prepared.manifest.size || '' })
|
|
373
501
|
json(res, 200, { snap: tracker.snapshot(session), dir: r.dir, title: t, screens: r.screens, license: prepared.manifest.license, kind: prepared.manifest.kind, flags: prepared.flags.slice(0, 20) })
|
|
374
502
|
} catch (e) {
|
|
375
503
|
log(e)
|
|
@@ -379,7 +507,7 @@ export function createLibraryRoutes({ admit, readBody, json, tracker, library, i
|
|
|
379
507
|
|
|
380
508
|
async function inspire(req, res) {
|
|
381
509
|
if (!admit(req, res)) return
|
|
382
|
-
if (req.method === 'GET') return json(res, 200, { items: inspiration.list() })
|
|
510
|
+
if (req.method === 'GET') return json(res, 200, { items: inspiration.list(), trash: inspiration.trashList() })
|
|
383
511
|
if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
|
|
384
512
|
let body
|
|
385
513
|
try { body = await readBody(req, MAX_IMG_BODY) } catch (e) { return json(res, 400, { error: e.message }) }
|
|
@@ -389,11 +517,14 @@ export function createLibraryRoutes({ admit, readBody, json, tracker, library, i
|
|
|
389
517
|
else if (body.op === 'addUrl') item = inspiration.addUrl({ url: body.url, name: body.name })
|
|
390
518
|
else if (body.op === 'addStyle') item = inspiration.addStyle({ name: body.name, style: body.style, mid: body.mid })
|
|
391
519
|
else if (body.op === 'addMarket') item = inspiration.addMarket({ mid: body.mid, mtype: body.mtype, name: body.name })
|
|
520
|
+
else if (body.op === 'addComponent') item = inspiration.addComponent({ mid: body.mid })
|
|
521
|
+
else if (body.op === 'trashImage') inspiration.trashImage(String(body.id || ''))
|
|
522
|
+
else if (body.op === 'restoreImage') inspiration.restoreImage(String(body.id || ''))
|
|
392
523
|
else if (body.op === 'remove') inspiration.remove(String(body.id || ''))
|
|
393
524
|
else return json(res, 400, { error: 'bad op' })
|
|
394
|
-
json(res, 200, { item, items: inspiration.list() })
|
|
525
|
+
json(res, 200, { item, items: inspiration.list(), trash: inspiration.trashList() })
|
|
395
526
|
} catch (e) {
|
|
396
|
-
json(res, 400, { error: e.message })
|
|
527
|
+
json(res, 400, { error: e.message, code: e.code })
|
|
397
528
|
}
|
|
398
529
|
}
|
|
399
530
|
|
|
@@ -410,7 +541,9 @@ export function createLibraryRoutes({ admit, readBody, json, tracker, library, i
|
|
|
410
541
|
// 一键投稿的错误:auth 要重新连 GitHub(不用 401,免得和 DSH 自己的登录混了)
|
|
411
542
|
const ghFail = (res, e) => {
|
|
412
543
|
if (!(e instanceof GhError)) { log(e); return json(res, 500, { error: 'device' }) }
|
|
413
|
-
|
|
544
|
+
const code = ['auth', 'who', 'issueMismatch', 'alreadySubmitted', 'branchChanged'].includes(e.code) ? 409
|
|
545
|
+
: e.code === 'busy' ? 429 : e.code === 'issueNumber' ? 400 : e.code === 'notUploaded' || e.status === 404 ? 404 : 502
|
|
546
|
+
json(res, code, { error: e.code, msg: String(e.message || '').slice(0, 200), status: e.status || 0, ...(e.progress ? { progress: e.progress } : {}) })
|
|
414
547
|
}
|
|
415
548
|
|
|
416
549
|
/** GET:连没连 GitHub、正在等输的码;POST { op: start | cancel | logout } */
|
|
@@ -429,23 +562,62 @@ export function createLibraryRoutes({ admit, readBody, json, tracker, library, i
|
|
|
429
562
|
} catch (e) { ghFail(res, e) }
|
|
430
563
|
}
|
|
431
564
|
|
|
432
|
-
/** POST { path }:把刚才「分享到市场」打好的包直接投上去 → { url }(投稿 issue) */
|
|
433
565
|
async function submit(req, res) {
|
|
434
566
|
if (!admit(req, res)) return
|
|
435
567
|
if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
|
|
436
568
|
let body
|
|
437
569
|
try { body = await readBody(req) } catch (e) { return json(res, 400, { error: e.message }) }
|
|
570
|
+
const op = body.op || 'submit'
|
|
571
|
+
if (!['submit', 'verify', 'status', 'withdraw'].includes(op)) return json(res, 400, { error: 'bad op' })
|
|
572
|
+
if (op === 'withdraw' && (typeof body.login !== 'string' || !/^[a-z0-9-]{1,39}$/i.test(body.login))) return json(res, 400, { error: 'loginRequired' })
|
|
438
573
|
const file = String(body.path || '')
|
|
439
|
-
const s =
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
574
|
+
const s = records.read(zipKey(file))
|
|
575
|
+
if (!s) return json(res, 404, { error: 'notShared' })
|
|
576
|
+
const { pack, share } = s
|
|
577
|
+
const { title, ref, slug, license, manualRef } = share
|
|
578
|
+
const as = typeof body.login === 'string' ? body.login.slice(0, 39) : ''
|
|
443
579
|
try {
|
|
444
|
-
const
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
580
|
+
const previous = knownIssue(s)
|
|
581
|
+
if (op === 'verify' || op === 'status') {
|
|
582
|
+
const found = op === 'verify'
|
|
583
|
+
? await github.verify({ issue: body.issue, pack, manualRef, ref, previous })
|
|
584
|
+
: previous && body.refresh === true ? await github.verify({ issue: previous.number, pack, previous }) : previous
|
|
585
|
+
const issue = found && found !== previous ? rememberIssue(s, file, found) : found
|
|
586
|
+
return json(res, 200, { ok: true, issue, progress: progressOf(s) })
|
|
587
|
+
}
|
|
588
|
+
if (op === 'withdraw') {
|
|
589
|
+
if (previous) throw new GhError('alreadySubmitted')
|
|
590
|
+
const progress = await github.withdraw({ pack, manualRef, as })
|
|
591
|
+
rememberProgress(s, file, progress)
|
|
592
|
+
return json(res, 200, { ok: true, progress })
|
|
593
|
+
}
|
|
594
|
+
// A manually verified issue also counts before invoking any write against GitHub.
|
|
595
|
+
if (previous) {
|
|
596
|
+
const issue = rememberIssue(s, file, await github.verify({ issue: previous.number, pack, previous }))
|
|
597
|
+
return json(res, 200, { ok: true, ...issue, issue, again: true, progress: progressOf(s) })
|
|
598
|
+
}
|
|
599
|
+
let zip
|
|
600
|
+
try {
|
|
601
|
+
const st = fs.statSync(file)
|
|
602
|
+
if (st.isFile()) {
|
|
603
|
+
if (st.size > ZIP_MAX) throw new GhError('big')
|
|
604
|
+
zip = fs.readFileSync(file)
|
|
605
|
+
}
|
|
606
|
+
} catch (e) { if (e instanceof GhError) throw e } // A deleted zip still allows status and withdrawal.
|
|
607
|
+
if (!zip || crypto.createHash('sha256').update(zip).digest('hex') !== s.zipSha) return json(res, 404, { error: 'notShared' })
|
|
608
|
+
const r = await github.submit({ zip, title, ref, slug, license, pack, manualRef, as })
|
|
609
|
+
const issue = rememberIssue(s, file, { number: r.number, url: r.url, state: r.state || 'open', login: r.login })
|
|
610
|
+
rememberProgress(s, file, r.progress)
|
|
611
|
+
json(res, 200, { ok: true, ...r, issue })
|
|
612
|
+
} catch (e) {
|
|
613
|
+
if (e instanceof GhError) {
|
|
614
|
+
rememberProgress(s, file, e.progress)
|
|
615
|
+
e.progress ||= progressOf(s)
|
|
616
|
+
if (e.progress?.issue) rememberIssue(s, file, e.progress.issue)
|
|
617
|
+
}
|
|
618
|
+
ghFail(res, e)
|
|
619
|
+
}
|
|
448
620
|
}
|
|
449
621
|
|
|
450
|
-
return { works, workthumb, pack, usezip, inspire, inspimg, ghauth, submit }
|
|
622
|
+
return { works, workthumb, renderMarketThumb, pack, usezip, inspire, inspimg, ghauth, submit }
|
|
451
623
|
}
|