-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstorage.osl
More file actions
394 lines (367 loc) · 11.1 KB
/
Copy pathstorage.osl
File metadata and controls
394 lines (367 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
97
98
99
100
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
133
134
135
136
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
def ensureDirectories() boolean (
boolean result = fs.mkdirAll(projectsDir)
result = result and fs.mkdirAll(usersDir)
result = result and fs.mkdirAll(sessionsDir)
result = result and fs.mkdirAll(commentsDir)
result = result and fs.mkdirAll(reportsDir)
result = result and fs.mkdirAll(notificationsDir)
result = result and fs.mkdirAll(activityDir)
result = result and fs.mkdirAll(userSettingsDir)
result = result and fs.mkdirAll(tmpDir)
result = result and fs.mkdirAll(stagingDir)
result = result and fs.mkdirAll(thumbnailsDir)
result = result and fs.mkdirAll(quotaDir)
result = result and fs.mkdirAll(agreementAcceptDir)
result = result and fs.mkdirAll(extensionsDir)
result = result and fs.mkdirAll(releasesDir)
result = result and fs.mkdirAll(safetyDir)
result = result and fs.mkdirAll(trashDir)
result = result and fs.mkdirAll(gameSavesDir)
result = result and fs.mkdirAll(gameSaveUsageDir)
result = result and fs.mkdirAll(gameEntitlementsDir)
result = result and fs.mkdirAll(gameInventoryDir)
result = result and fs.mkdirAll(backpackDir)
return result
)
def projectSizeBytes(object project) number (
if project.contains("sizeBytes") and project.sizeBytes != null (
return project.sizeBytes.toNum()
)
return 0
)
def projectHistoryBytes(object project) number (
if project.contains("historyBytes") and project.historyBytes != null (
return project.historyBytes.toNum()
)
return 0
)
def projectStorageBytes(object project) number (
return projectSizeBytes(project) + projectHistoryBytes(project)
)
def weekViewsOf(object project) number (
if !project.contains("viewHistory") or project.viewHistory == null (
return 0
)
object history = project.viewHistory.assert(object)
array keys = history.getKeys()
number today = (timestamp / 86400000).toInt()
number total = 0
for i keys.len (
string k = keys[i].toStr()
if today - k.toNum() < 7 (
total += history[k].toNum()
)
)
return total
)
def loadAssetsIndex() object (
auto result = readFile(assetsIndexFile)
if !result.ok (
return {assets: {}}
)
return result.data.assert(object)
)
def saveAssetsIndex(object index) boolean (
return writeFile(assetsIndexFile, index)
)
def readFile(string path) object (
if !fs.exists(path) (
return {ok: false, error: "file not found"}
)
string content = fs.readFile(path)
auto parsed = json.parse(content, {type: "object"})
if parsed.isErr() (
return {ok: false, error: "invalid json"}
)
return {ok: true, data: parsed.unwrap()}
)
def writeFile(string path, object data) boolean (
string tmpPath = path ++ ".tmp." ++ randomString(8)
if !fs.writeFile(tmpPath, json.stringify(data)) (
return false
)
if !fs.rename(tmpPath, path) (
fs.remove(tmpPath)
return false
)
return true
)
def loadIndex() object (
auto result = readFile(indexFile)
if !result.ok (
return {projects: {}}
)
return result.data.assert(object)
)
def saveIndex(object index) boolean (
return writeFile(indexFile, index)
)
def indexEntryFromProject(object project) object (
array loves = project.loves.assert(array)
array broken = []
if project.contains("brokenhearts") (
broken = project.brokenhearts.assert(array)
)
array tags = []
if project.contains("tags") and project.tags != null (
tags = project.tags.assert(array)
)
array contributors = []
if project.contains("collaborators") and project.collaborators != null (
array collaborators = project.collaborators.assert(array)
for i collaborators.len (
object collaborator = collaborators[i].assert(object)
string username = collaborator.username.toStr().strip()
if username != "" and !contributors.contains(username) (
contributors.append(username)
)
)
)
if project.contains("mergedContributors") and project.mergedContributors != null (
array merged = project.mergedContributors.assert(array)
for i merged.len (
string username = merged[i].toStr().strip()
if username != "" and normalizeUsername(username) != normalizeUsername(project.owner.toStr()) and !contributors.contains(username) (
contributors.append(username)
)
)
)
number acceptedChanges = 0
if project.contains("acceptedChanges") and project.acceptedChanges != null (
acceptedChanges = project.acceptedChanges.toNum()
)
string ownerId = ""
if project.contains("ownerId") and project.ownerId != null (
ownerId = project.ownerId.toStr()
)
if ownerId == "" and project.contains("owner") and project.owner != null (
ownerId = userIdForUsername(project.owner.toStr())
)
string remixRoot = ""
if project.contains("remixRoot") and project.remixRoot != null (
remixRoot = project.remixRoot.toStr()
)
string groupTag = ""
if project.contains("groupTag") and project.groupTag != null (
groupTag = normalizeGroupTag(project.groupTag.toStr())
)
string vanitySlug = ""
if project.contains("vanitySlug") and project.vanitySlug != null (
vanitySlug = project.vanitySlug.toStr()
)
// vanityClaim holds the URL while it's lapsed. vanitySlug is what lists link
// to, so it's empty until the owner's plan includes vanity URLs again.
return {
id: project.id,
owner: project.owner,
ownerId: ownerId,
ownerKey: normalizeUsername(project.owner.toStr()),
groupTag: groupTag,
vanityClaim: vanitySlug,
vanitySlug: if (project.vanityLapsedAt ?? 0).toNum() > 0 then "" else vanitySlug,
title: project.title,
thumbUrl: thumbUrlFor(project),
views: project.views,
loveCount: loves.len,
brokenHeartCount: broken.len,
remixParent: project.remixParent,
remixRoot: remixRoot,
contributionOnly: projectContributionOnlyFlag(project),
shared: project.shared,
visibility: projectVisibility(project),
price: projectPrice(project),
revenue: projectRevenue(project),
created: project.created,
edited: project.edited,
sharedAt: project.sharedAt,
sizeBytes: projectSizeBytes(project),
historyBytes: projectHistoryBytes(project),
weekViews: weekViewsOf(project),
teamSize: contributors.len + 1,
contributors: contributors,
acceptedChanges: acceptedChanges,
tags: tags
}
)
def refreshIndexUrls() boolean (
array projects = projectSearchDocs.all()
boolean changed = false
for i projects.len (
object entry = projects[i].assert(object)
string thumbUrl = thumbUrlFor(entry)
if entry.thumbUrl.toStr() != thumbUrl (
entry.thumbUrl = thumbUrl
putDocument(projectSearchDocs, entry["_id"].toStr(), entry)
changed = true
)
)
if changed (
sharedProjectsCache.delete("projects")
)
return true
)
def upsertIndexEntry(object project) boolean (
return syncSearchProject(project)
)
def removeFromIndex(string id) boolean (
return removeSearchProject(id)
)
def loadProject(string id) object (
if !isSafePathPart(id) (
return {ok: false, error: "invalid project id"}
)
auto result = readFile(projectsDir ++ id ++ ".json")
if !result.ok (
string projectPath = projectsDir ++ id ++ ".json"
boolean missing = storagefs.Missing(projectPath)
return {ok: false, error: if missing then "project not found" else "project read failed", missing: missing, retryable:!missing}
)
object project = result.data.assert(object)
if (!project.contains("ownerId") or project.ownerId == null or project.ownerId.toStr() == "") and project.contains("owner") and project.owner != null (
string uid = userIdForUsername(project.owner.toStr())
if uid != "" (
project.ownerId = uid
)
)
return {ok: true, project: project}
)
def saveProject(object project) boolean (
string id = project.id.toStr()
if !isSafePathPart(id) (
return false
)
// Keep the inactivity scan and eviction consistent with published edits.
sync.lock("r2-upload-queue")
boolean saved = writeFile(projectsDir ++ id ++ ".json", project)
sync.unlock("r2-upload-queue")
return saved
)
def deleteProjectFile(string id) boolean (
if !isSafePathPart(id) (
return false
)
return fs.remove(projectsDir ++ id ++ ".json")
)
def defaultProfile(string username) object (
return {
userId: "",
username: username,
bio: "",
featuredProject: "",
followers: [],
following: [],
created: timestamp
}
)
def profileExists(string username) boolean (
string key = normalizeUsername(username)
return isSafePathPart(key) and fs.exists(usersDir ++ key ++ ".json")
)
def loadProfile(string username) object (
string key = normalizeUsername(username)
if !isSafePathPart(key) (
return defaultProfile(username)
)
auto result = readFile(usersDir ++ key ++ ".json")
if !result.ok (
return defaultProfile(username)
)
return result.data.assert(object)
)
def saveProfile(object profile) boolean (
string key = normalizeUsername(profile.username.toStr())
if !isSafePathPart(key) (
return false
)
boolean saved = writeFile(usersDir ++ key ++ ".json", profile)
syncSearchProfile(profile)
return saved
)
def loadActivity(string username) array (
string key = normalizeUsername(username)
if !isSafePathPart(key) (
return []
)
auto result = readFile(activityDir ++ key ++ ".json")
if !result.ok (
return []
)
object data = result.data.assert(object)
return data.items.assert(array)
)
def addActivity(string username, string activityType, object extra) (
string key = normalizeUsername(username)
if !isSafePathPart(key) (
return
)
object item = {type: activityType, actor: key, created: timestamp}
array extraKeys = extra.getKeys()
for i extraKeys.len (
string k = extraKeys[i].toStr()
item[k] = extra[k]
)
array items = [item]
array existing = loadActivity(key)
for i existing.len (
if items.len < 50 (
items.append(existing[i])
)
)
writeFile(activityDir ++ key ++ ".json", {items: items})
)
def loadCommentsLegacy(string key) array (
auto result = readFile(commentsDir ++ key ++ ".json")
if !result.ok (
return []
)
object data = result.data.assert(object)
return data.comments.assert(array)
)
def loadComments(string key) array (
if !isSafePathPart(key) (
return []
)
string path = commentsDir ++ key ++ ".jsonl"
if !fs.exists(path) (
return loadCommentsLegacy(key)
)
array lines = fs.readFile(path).split("\n")
array out = []
for i lines.len (
string line = lines[i].toStr().strip()
if line != "" (
auto parsed = json.parse(line, {type: "object"})
if !parsed.isErr() (
out.append(parsed.unwrap())
)
)
)
return out
)
def saveComments(string key, array comments) boolean (
if !isSafePathPart(key) (
return false
)
string out = ""
for i comments.len (
out ++= json.stringify(comments[i]) ++ "\n"
)
string path = commentsDir ++ key ++ ".jsonl"
string tmpPath = path ++ ".tmp." ++ randomString(8)
if !fs.writeFile(tmpPath, out) (
return false
)
boolean ok = fs.rename(tmpPath, path)
if !ok (
fs.remove(tmpPath)
return false
)
fs.remove(commentsDir ++ key ++ ".json")
return true
)
def appendComment(string key, object comment) boolean (
if !isSafePathPart(key) (
return false
)
return fs.appendToFile(commentsDir ++ key ++ ".jsonl", json.stringify(comment) ++ "\n")
)