liquid_feedback_frontend
diff 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 |
line diff
1.1 --- a/app/main/draft/_action/add.lua Mon Dec 09 15:54:57 2019 +0100 1.2 +++ b/app/main/draft/_action/add.lua Mon Feb 10 21:10:49 2020 +0100 1.3 @@ -21,7 +21,68 @@ 1.4 draft_text = abstract .. "<!--END_OF_ABSTRACT-->" .. draft_text 1.5 end 1.6 1.7 -return Draft:update_content( 1.8 +if config.attachments then 1.9 + local file_upload_session = param.get("file_upload_session") 1.10 + file_upload_session = string.gsub(file_upload_session, "[^A-Za-z0-9]", "") 1.11 + local file_uploads = json.array() 1.12 + local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. ".json") 1.13 + local fh = io.open(filename, "r") 1.14 + if fh then 1.15 + file_uploads = json.import(fh:read("*a")) 1.16 + end 1.17 + for i, file_upload in ipairs(file_uploads) do 1.18 + if param.get("file_upload_delete_" .. file_upload.id, atom.boolean) then 1.19 + for j = i, #file_uploads - 1 do 1.20 + file_uploads[j] = file_uploads[j+1] 1.21 + end 1.22 + file_uploads[#file_uploads] = nil 1.23 + end 1.24 + end 1.25 + local convert_func = config.attachments.convert_func 1.26 + local last_id = param.get("file_upload_last_id", atom.number) 1.27 + if last_id and last_id > 0 then 1.28 + if last_id > 1024 then 1.29 + last_id = 1024 1.30 + end 1.31 + for i = 1, last_id do 1.32 + local file = param.get("file_" .. i) 1.33 + if file and #file > 0 then 1.34 + local id = multirand.string( 1.35 + 32, 1.36 + '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' 1.37 + ) 1.38 + local data, err, status = convert_func(file) 1.39 + if status ~= 0 or data == nil then 1.40 + slot.put_into("error", _"Error while converting image. Please note, that only JPG files are supported!") 1.41 + return false 1.42 + end 1.43 + local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. "-" .. id .. ".jpg") 1.44 + local fh = assert(io.open(filename, "w")) 1.45 + fh:write(file) 1.46 + fh:write("\n") 1.47 + fh:close() 1.48 + local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. "-" .. id .. ".preview.jpg") 1.49 + local fh = assert(io.open(filename, "w")) 1.50 + fh:write(data) 1.51 + fh:write("\n") 1.52 + fh:close() 1.53 + table.insert(file_uploads, json.object{ 1.54 + id = id, 1.55 + filename = filename, 1.56 + title = param.get("title_" .. i), 1.57 + description = param.get("description_" .. i) 1.58 + }) 1.59 + end 1.60 + end 1.61 + end 1.62 + local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. ".json") 1.63 + local fh = assert(io.open(filename, "w")) 1.64 + fh:write(json.export(file_uploads)) 1.65 + fh:write("\n") 1.66 + fh:close() 1.67 +end 1.68 + 1.69 +local draft_id = Draft:update_content( 1.70 app.session.member.id, 1.71 param.get("initiative_id", atom.integer), 1.72 param.get("formatting_engine"), 1.73 @@ -29,3 +90,66 @@ 1.74 nil, 1.75 param.get("preview") or param.get("edit") 1.76 ) 1.77 + 1.78 +if draft_id and config.attachments then 1.79 + local file_upload_session = param.get("file_upload_session") 1.80 + file_upload_session = string.gsub(file_upload_session, "[^A-Za-z0-9]", "") 1.81 + 1.82 + local draft_attachments = DraftAttachment:new_selector() 1.83 + :add_where{ "draft_attachment.draft_id = ?", draft_id } 1.84 + :exec() 1.85 + 1.86 + for i, draft_attachment in ipairs(draft_attachments) do 1.87 + if param.get("file_delete_" .. draft_attachment.file_id, atom.boolean) then 1.88 + draft_attachment:destroy() 1.89 + end 1.90 + end 1.91 + 1.92 + local file_uploads = json.array() 1.93 + local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. ".json") 1.94 + local fh = io.open(filename, "r") 1.95 + if fh then 1.96 + file_uploads = json.import(fh:read("*a")) 1.97 + end 1.98 + for i, file_upload in ipairs(file_uploads) do 1.99 + local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. "-" .. file_upload.id .. ".jpg") 1.100 + local data 1.101 + local fh = io.open(filename, "r") 1.102 + if fh then 1.103 + data = fh:read("*a") 1.104 + end 1.105 + local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. "-" .. file_upload.id .. ".preview.jpg") 1.106 + local data_preview 1.107 + local fh = io.open(filename, "r") 1.108 + if fh then 1.109 + data_preview = fh:read("*a") 1.110 + end 1.111 + 1.112 + local hash = moonhash.sha3_512(data) 1.113 + 1.114 + local file = File:new_selector() 1.115 + :add_where{ "hash = ?", hash } 1.116 + :add_where{ "content_type = ?", "image/jpeg" } 1.117 + :optional_object_mode() 1.118 + :exec() 1.119 + 1.120 + if not file then 1.121 + file = File:new() 1.122 + file.content_type = "image/jpeg" 1.123 + file.hash = hash 1.124 + file.data = data 1.125 + file.preview_content_type = "image/jpeg" 1.126 + file.preview_data = data_preview 1.127 + file:save() 1.128 + end 1.129 + 1.130 + local draft_attachment = DraftAttachment:new() 1.131 + draft_attachment.draft_id = draft_id 1.132 + draft_attachment.file_id = file.id 1.133 + draft_attachment.title = file_upload.title 1.134 + draft_attachment.description = file_upload.description 1.135 + draft_attachment:save() 1.136 + end 1.137 +end 1.138 + 1.139 +return draft_id and true or false