liquid_feedback_frontend

annotate app/main/draft/_action/add.lua @ 1495:17e7082c377a

Added image attachments for initiatives
author bsw
date Mon Feb 10 21:10:49 2020 +0100 (2020-02-10)
parents 32cc544d5a5b
children ed3c40911ae1
rev   line source
bsw/jbe@1309 1 local draft_text = param.get("content")
bsw/jbe@1309 2
bsw/jbe@1309 3 if not draft_text then
bsw/jbe@1309 4 return false
bsw/jbe@1309 5 end
bsw/jbe@1309 6
bsw/jbe@1309 7 local draft_text = util.wysihtml_preproc(draft_text)
bsw/jbe@1309 8
bsw/jbe@1309 9 local valid_html, error_message = util.html_is_safe(draft_text)
bsw/jbe@1309 10 if not valid_html then
bsw/jbe@1309 11 slot.put_into("error", _("Draft contains invalid formatting or character sequence: #{error_message}", { error_message = error_message }) )
bsw/jbe@1309 12 return false
bsw/jbe@1309 13 end
bsw/jbe@1309 14
bsw/jbe@1309 15 if config.initiative_abstract then
bsw/jbe@1309 16 local abstract = param.get("abstract")
bsw/jbe@1309 17 if not abstract then
bsw/jbe@1309 18 return false
bsw/jbe@1309 19 end
bsw/jbe@1309 20 abstract = encode.html(abstract)
bsw/jbe@1309 21 draft_text = abstract .. "<!--END_OF_ABSTRACT-->" .. draft_text
bsw/jbe@1309 22 end
bsw/jbe@1309 23
bsw@1495 24 if config.attachments then
bsw@1495 25 local file_upload_session = param.get("file_upload_session")
bsw@1495 26 file_upload_session = string.gsub(file_upload_session, "[^A-Za-z0-9]", "")
bsw@1495 27 local file_uploads = json.array()
bsw@1495 28 local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. ".json")
bsw@1495 29 local fh = io.open(filename, "r")
bsw@1495 30 if fh then
bsw@1495 31 file_uploads = json.import(fh:read("*a"))
bsw@1495 32 end
bsw@1495 33 for i, file_upload in ipairs(file_uploads) do
bsw@1495 34 if param.get("file_upload_delete_" .. file_upload.id, atom.boolean) then
bsw@1495 35 for j = i, #file_uploads - 1 do
bsw@1495 36 file_uploads[j] = file_uploads[j+1]
bsw@1495 37 end
bsw@1495 38 file_uploads[#file_uploads] = nil
bsw@1495 39 end
bsw@1495 40 end
bsw@1495 41 local convert_func = config.attachments.convert_func
bsw@1495 42 local last_id = param.get("file_upload_last_id", atom.number)
bsw@1495 43 if last_id and last_id > 0 then
bsw@1495 44 if last_id > 1024 then
bsw@1495 45 last_id = 1024
bsw@1495 46 end
bsw@1495 47 for i = 1, last_id do
bsw@1495 48 local file = param.get("file_" .. i)
bsw@1495 49 if file and #file > 0 then
bsw@1495 50 local id = multirand.string(
bsw@1495 51 32,
bsw@1495 52 '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
bsw@1495 53 )
bsw@1495 54 local data, err, status = convert_func(file)
bsw@1495 55 if status ~= 0 or data == nil then
bsw@1495 56 slot.put_into("error", _"Error while converting image. Please note, that only JPG files are supported!")
bsw@1495 57 return false
bsw@1495 58 end
bsw@1495 59 local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. "-" .. id .. ".jpg")
bsw@1495 60 local fh = assert(io.open(filename, "w"))
bsw@1495 61 fh:write(file)
bsw@1495 62 fh:write("\n")
bsw@1495 63 fh:close()
bsw@1495 64 local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. "-" .. id .. ".preview.jpg")
bsw@1495 65 local fh = assert(io.open(filename, "w"))
bsw@1495 66 fh:write(data)
bsw@1495 67 fh:write("\n")
bsw@1495 68 fh:close()
bsw@1495 69 table.insert(file_uploads, json.object{
bsw@1495 70 id = id,
bsw@1495 71 filename = filename,
bsw@1495 72 title = param.get("title_" .. i),
bsw@1495 73 description = param.get("description_" .. i)
bsw@1495 74 })
bsw@1495 75 end
bsw@1495 76 end
bsw@1495 77 end
bsw@1495 78 local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. ".json")
bsw@1495 79 local fh = assert(io.open(filename, "w"))
bsw@1495 80 fh:write(json.export(file_uploads))
bsw@1495 81 fh:write("\n")
bsw@1495 82 fh:close()
bsw@1495 83 end
bsw@1495 84
bsw@1495 85 local draft_id = Draft:update_content(
bsw@1208 86 app.session.member.id,
bsw@1208 87 param.get("initiative_id", atom.integer),
bsw@1208 88 param.get("formatting_engine"),
bsw/jbe@1309 89 draft_text,
bsw@1208 90 nil,
bsw@1208 91 param.get("preview") or param.get("edit")
bsw@1208 92 )
bsw@1495 93
bsw@1495 94 if draft_id and config.attachments then
bsw@1495 95 local file_upload_session = param.get("file_upload_session")
bsw@1495 96 file_upload_session = string.gsub(file_upload_session, "[^A-Za-z0-9]", "")
bsw@1495 97
bsw@1495 98 local draft_attachments = DraftAttachment:new_selector()
bsw@1495 99 :add_where{ "draft_attachment.draft_id = ?", draft_id }
bsw@1495 100 :exec()
bsw@1495 101
bsw@1495 102 for i, draft_attachment in ipairs(draft_attachments) do
bsw@1495 103 if param.get("file_delete_" .. draft_attachment.file_id, atom.boolean) then
bsw@1495 104 draft_attachment:destroy()
bsw@1495 105 end
bsw@1495 106 end
bsw@1495 107
bsw@1495 108 local file_uploads = json.array()
bsw@1495 109 local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. ".json")
bsw@1495 110 local fh = io.open(filename, "r")
bsw@1495 111 if fh then
bsw@1495 112 file_uploads = json.import(fh:read("*a"))
bsw@1495 113 end
bsw@1495 114 for i, file_upload in ipairs(file_uploads) do
bsw@1495 115 local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. "-" .. file_upload.id .. ".jpg")
bsw@1495 116 local data
bsw@1495 117 local fh = io.open(filename, "r")
bsw@1495 118 if fh then
bsw@1495 119 data = fh:read("*a")
bsw@1495 120 end
bsw@1495 121 local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. "-" .. file_upload.id .. ".preview.jpg")
bsw@1495 122 local data_preview
bsw@1495 123 local fh = io.open(filename, "r")
bsw@1495 124 if fh then
bsw@1495 125 data_preview = fh:read("*a")
bsw@1495 126 end
bsw@1495 127
bsw@1495 128 local hash = moonhash.sha3_512(data)
bsw@1495 129
bsw@1495 130 local file = File:new_selector()
bsw@1495 131 :add_where{ "hash = ?", hash }
bsw@1495 132 :add_where{ "content_type = ?", "image/jpeg" }
bsw@1495 133 :optional_object_mode()
bsw@1495 134 :exec()
bsw@1495 135
bsw@1495 136 if not file then
bsw@1495 137 file = File:new()
bsw@1495 138 file.content_type = "image/jpeg"
bsw@1495 139 file.hash = hash
bsw@1495 140 file.data = data
bsw@1495 141 file.preview_content_type = "image/jpeg"
bsw@1495 142 file.preview_data = data_preview
bsw@1495 143 file:save()
bsw@1495 144 end
bsw@1495 145
bsw@1495 146 local draft_attachment = DraftAttachment:new()
bsw@1495 147 draft_attachment.draft_id = draft_id
bsw@1495 148 draft_attachment.file_id = file.id
bsw@1495 149 draft_attachment.title = file_upload.title
bsw@1495 150 draft_attachment.description = file_upload.description
bsw@1495 151 draft_attachment:save()
bsw@1495 152 end
bsw@1495 153 end
bsw@1495 154
bsw@1495 155 return draft_id and true or false

Impressum / About Us