~tsileo/blobpad

c2eef044f6bc3e4f6aa58a35d6c468f68f765dac — Thomas Sileo 4 years ago 6c30c42
Cleanup
1 files changed, 24 insertions(+), 29 deletions(-)

M app.lua
M app.lua => app.lua +24 -29
@@ 5,32 5,32 @@ local router = require('router').new()
local ft = require('filetree')
local docstore = require('docstore')

local notes_col = "notes"
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)
  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")
  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)
      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)
        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[...]"
    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


@@ 46,17 46,16 @@ function _expand_note(d, pointers, size, excerpt)
end
 
router:get('/', function(params)
  local col = docstore.col(notes_col)
  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
  local qs = args:get('qs')
  if qs ~= '' then
    -- compute the "text fields" on the fly for the text search
    local tf = {"title", "content"}
    local tf = {'title', 'content'}
    -- setup the search function
    sf = function(doc) return docstore.text_search(doc, qs, tf) end
  end


@@ 74,22 73,20 @@ router:get('/', function(params)
  end

  -- do the query
  local docs, pointers, cursor, stats  = col:query(args:get("cursor"), 100, sf, sort_index)
  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, col = notes_col, collections = {}, docs = jdocs }))
  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 col = docstore.col(notes_col)
  local cdoc = {}
  local args = app.request:args()

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


@@ 101,12 98,11 @@ end)


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

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


@@ 115,33 111,32 @@ router:get('/new', function(params)
end)

router:post('/new', function(params)
  local col = docstore.col(notes_col)
  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
  if f:get('attachments') ~= '' then
    attachments = extra.split(f:get('attachments'), ',')
  end
  local pinned = false
  if f:get("pinned") == "on" then
  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
  if app.request:args():get('cid') == "" then
    col:insert(dat)
    app.response:redirect("/api/apps/" .. blobstash.app_id .. "/")
    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 .. "/")
    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)
  local col = docstore.col(notes_col)
  col:remove(app.request:args():get("cid")) 
  app.response:redirect("/api/apps/" .. blobstash.app_id .. "/")
  col:remove(app.request:args():get('cid')) 
  app.response:redirect('/api/apps/' .. blobstash.app_id .. '/')
end)

router:run()