~tsileo/blobpad

ref: c2eef044f6bc3e4f6aa58a35d6c468f68f765dac blobpad/app.lua -rw-r--r-- 4.5 KiB
c2eef044Thomas Sileo Cleanup 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
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
local template = require('template')
local extra = require('extra')
local json = require('json')
local router = require('router').new()
local ft = require('filetree')
local docstore = require('docstore')

local col = docstore.col('notes')

-- File upload endpoint used when creating a new note
router:post('/attachment', function(params)
  local f = app.request:file('file')
  local ref = ft.upload_file(f.filename, f.contents)
  app.response:write('@filetree/ref:' .. ref)
end)

function _expand_note(d, pointers, size, excerpt)
  d.updated = extra.format_datetime(d._updated, '2006-01-02T15:04:05Z07:00', 'Jan 02, 2006 @ 15:04 MST')
  if d.attachments ~= nil then
    for _, attachment in ipairs(d.attachments) do
      local a = pointers[attachment]
      if a ~= nil then
      if a.file_type == 'image' then
        d.content, _ = string.gsub(d.content, attachment, function(w) return '<img src="' .. a.url .. '&w=' .. tostring(size) .. '">' end)
      else
        d.content, _ = string.gsub(d.content, attachment, function(w) return '<a href="' .. a.url .. '&dl=1">' .. a.name .. '</a>' end)
      end
      end
    end
  end
  -- Make an excerpt
  if excerpt and string.len(d.content) > 750 then
    d.excerpt = string.sub(d.content, 1, 750) .. '\n[...]'
    -- Check if we need to close a huge code block
    local codeblocks = 0
    for m in string.gfind(d.excerpt, "'''") do
      codeblocks = codeblocks + 1
    end
    if codeblocks %2 ~= 0 then
      d.excerpt = d.excerpt .. "'''"
    end
  else
    -- No excerpt needed
    d.excerpt = d.content
  end
end
 
router:get('/', function(params)
  local args = app.request:args()

  -- default search func
  local sf = function(doc) return not doc.pinned end

  -- check if a text search is requested
  local qs = args:get('qs')
  if qs ~= '' then
    -- compute the "text fields" on the fly for the text search
    local tf = {'title', 'content'}
    -- setup the search function
    sf = function(doc) return docstore.text_search(doc, qs, tf) end
  end

  local jdocs = {}
  -- most recently updated first
  local sort_index = '-_updated'

  if qs == "" then
    local docs, pointers, _, _ = col:query('', 50, function(doc) return doc.pinned end, sort_index)
    for _, d in ipairs(docs) do
      _expand_note(d, pointers, 800, true)
      table.insert(jdocs, {doc=d, js=json.encode(d)})
    end
  end

  -- do the query
  local docs, pointers, cursor, stats  = col:query(args:get('cursor'), 100, sf, sort_index)
  for _, d in ipairs(docs) do
    _expand_note(d, pointers, 800, true)
    table.insert(jdocs, {doc=d, js=json.encode(d)})
  end

  app.response:write(template.render('index.html', 'layout.html', { app_id = blobstash.app_id, stats = stats, qs = qs, collections = {}, docs = jdocs }))
end)

router:get('/view', function(params)
  local cdoc = {}
  local args = app.request:args()

  local cid = args:get('_id')
  if cid ~= "" then
    cdoc, pointers = col:get(cid)
    _expand_note(cdoc, pointers, 800, false)
    cdoc._id = cid
  end

  app.response:write(template.render('view.html', 'layout.html', { app_id = blobstash.app_id, cid = cid, cdoc = cdoc, js = json.encode(cdoc), col = notes_col }))
end)


router:get('/new', function(params)
  local cdoc = {}
  local args = app.request:args()

  -- check if a doc ID is requested for edit
  local cid = args:get('_id')
  if cid ~= "" then
    cdoc, _ = col:get(cid)
  end

  app.response:write(template.render('new.html', 'layout.html', { app_id = blobstash.app_id, cid = cid, cdoc = cdoc, col = notes_col }))
end)

router:post('/new', function(params)
  local f = app.request:form()
  -- explicitely use an array here, because if it's empty, Lua won't make the diff between a "array" table and a "object" table
  local attachments = json.new_array()
  if f:get('attachments') ~= '' then
    attachments = extra.split(f:get('attachments'), ',')
  end
  local pinned = false
  if f:get('pinned') == 'on' then
    pinned = true
  end
  local dat = {title = f:get('title'), content = f:get('content'), pinned = pinned, attachments = attachments}

  if app.request:args():get('cid') == "" then
    col:insert(dat)
    app.response:redirect('/api/apps/' .. blobstash.app_id .. '/')
  else
    col:update(app.request:args():get('cid'), dat) 
    if app.request:args():get('redirect') ~= 'no' then
      app.response:redirect('/api/apps/' .. blobstash.app_id .. '/')
    end
  end
end)

router:post('/remove', function(params)
  col:remove(app.request:args():get('cid')) 
  app.response:redirect('/api/apps/' .. blobstash.app_id .. '/')
end)

router:run()