~tsileo/blobstash-pwa

ref: 6bc76be7c73034a92029f64f8ff7d6622beffcf7 blobstash-pwa/app.lua -rw-r--r-- 2.1 KiB
6bc76be7Thomas Sileo Add LICENSE 2 years ago
                                                                                
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
local template = require('template')
local router = require('router').new()
local ft = require('filetree')

router:get('/', function(params)
  app.response:write(template.render_string([[
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>BlobStash</title>
    <link rel="manifest" href="/api/apps/{{.app_id}}/manifest.json">
<script>
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/api/apps/{{.app_id}}/service-worker.js')
  }
</script>
  </head>
  <body>
    <h2>BlobStash</h2>
    <p>This app only registers a sharing plugin.</p>
  </body>
</html>
  ]], {app_id = blobstash.app_id}))
end)

router:get('/manifest.json', function(params)
  app.response:headers():set("Content-Type", "application/json")
  app.response:write(template.render_string([[{
      "short_name": "BlobStash",
      "name": "BlobStash",
      "share_target": {
          "action": "/api/apps/{{.app_id}}/share-target",
          "enctype": "multipart/form-data",
          "method": "POST",
          "params": {"files": [{"name": "image", "accept": ["image/*"]}]}},
          "icons": [
                {"src": "https://sos-ch-dk-2.exo.io/hexaninja/blobstash.png", "sizes": "192x192", "type": "image/png"},
                {"src": "https://sos-ch-dk-2.exo.io/hexaninja/blobstash_512.png", "sizes": "512x512", "type": "image/png"}
           ],
           "start_url": "/api/apps/{{.app_id}}/",
           "background_color": "#ffffff",
           "display": "standalone",
           "scope": "/api/apps/{{.app_id}}/",
           "theme_color": "#ffffff"
       }]], {app_id = blobstash.app_id}))
end)

router:post("/share-target", function(params)
  local f = app.request:file('image')
  local new_parent, _ = ft.put_file_at({user_agent = "Android", message = "Uploaded " .. f.filename .. " via sharing"}, f.filename, f.contents, "Android", "/")
  app.response:write("OK")
end)

router:get("/service-worker.js", function(params)
  app.response:headers():set("Content-Type", "application/javascript")
  app.response:write('self.addEventListener("fetch", function (event) { event.respondWith(fetch(event.request)); });')
end)

router:run()