~tsileo/blobstash-pwa

blobstash-pwa/app.lua -rw-r--r-- 2.3 KiB
284eea23Thomas Sileo Update th SW 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
local template = require('template')
local router = require('router').new()
local ft = require('filetree')
local docstore = require('docstore')
local json = require('json')

router:get('/', function(params)
  app.response:write(template.render("index.html", {app_id = blobstash.app_id}))
end)

router:post('/checkin', function(params)
  local form = app.request:form()
  local checkin = {
    ts = tonumber(form:get("location_ts")),
    lat = tonumber(form:get("location_lat")),
    lng = tonumber(form:get("location_lng")),
    name = form:get("location_name"),
    device_id = app.request:headers():get("User-Agent"),
  }
  local col = docstore.col('gps_locations')
  col:insert(checkin)
  app.response:redirect("/api/apps/" .. blobstash.app_id .. "/")
end)

router:get('/manifest.json', function(params)
  app.response:headers():set("Content-Type", "application/json")
  app.response:write(json.encode{
    short_name = "BlobStash",
    name = "BlobStash",
    share_target = {
      action = "/api/apps/" .. blobstash.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/" .. blobstash.app_id .. "/",
    background_color = "#ffffff",
    theme_color = "#ffffff",
    display = "standalone",
    scope = "/api/apps/" .. blobstash.app_id .. "/",
  })
end)

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

function register_js(name)
  router:get("/" .. name, function(params)
    app.response:headers():set("Content-Type", "application/javascript")
    app.response:write(template.render(name, {app_id = blobstash.app_id}))
  end)
end

register_js("service-worker.js")

router:run()