bsw/jbe@1309: local draft_text = param.get("content") bsw/jbe@1309: bsw/jbe@1309: if not draft_text then bsw/jbe@1309: return false bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: local draft_text = util.wysihtml_preproc(draft_text) bsw/jbe@1309: bsw/jbe@1309: local valid_html, error_message = util.html_is_safe(draft_text) bsw/jbe@1309: if not valid_html then bsw/jbe@1309: slot.put_into("error", _("Draft contains invalid formatting or character sequence: #{error_message}", { error_message = error_message }) ) bsw/jbe@1309: return false bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: if config.initiative_abstract then bsw/jbe@1309: local abstract = param.get("abstract") bsw/jbe@1309: if not abstract then bsw/jbe@1309: return false bsw/jbe@1309: end bsw/jbe@1309: abstract = encode.html(abstract) bsw/jbe@1309: draft_text = abstract .. "" .. draft_text bsw/jbe@1309: end bsw/jbe@1309: bsw@1495: if config.attachments then bsw@1495: local file_upload_session = param.get("file_upload_session") bsw@1495: file_upload_session = string.gsub(file_upload_session, "[^A-Za-z0-9]", "") bsw@1495: local file_uploads = json.array() bsw@1495: local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. ".json") bsw@1495: local fh = io.open(filename, "r") bsw@1495: if fh then bsw@1495: file_uploads = json.import(fh:read("*a")) bsw@1495: end bsw@1495: for i, file_upload in ipairs(file_uploads) do bsw@1495: if param.get("file_upload_delete_" .. file_upload.id, atom.boolean) then bsw@1495: for j = i, #file_uploads - 1 do bsw@1495: file_uploads[j] = file_uploads[j+1] bsw@1495: end bsw@1495: file_uploads[#file_uploads] = nil bsw@1495: end bsw@1495: end bsw@1495: local convert_func = config.attachments.convert_func bsw@1495: local last_id = param.get("file_upload_last_id", atom.number) bsw@1495: if last_id and last_id > 0 then bsw@1495: if last_id > 1024 then bsw@1495: last_id = 1024 bsw@1495: end bsw@1495: for i = 1, last_id do bsw@1495: local file = param.get("file_" .. i) bsw@1495: if file and #file > 0 then bsw@1495: local id = multirand.string( bsw@1495: 32, bsw@1495: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' bsw@1495: ) bsw@1495: local data, err, status = convert_func(file) bsw@1495: if status ~= 0 or data == nil then bsw@1495: slot.put_into("error", _"Error while converting image. Please note, that only JPG files are supported!") bsw@1495: return false bsw@1495: end bsw@1495: local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. "-" .. id .. ".jpg") bsw@1495: local fh = assert(io.open(filename, "w")) bsw@1495: fh:write(file) bsw@1495: fh:write("\n") bsw@1495: fh:close() bsw@1495: local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. "-" .. id .. ".preview.jpg") bsw@1495: local fh = assert(io.open(filename, "w")) bsw@1495: fh:write(data) bsw@1495: fh:write("\n") bsw@1495: fh:close() bsw@1495: table.insert(file_uploads, json.object{ bsw@1495: id = id, bsw@1495: filename = filename, bsw@1495: title = param.get("title_" .. i), bsw@1495: description = param.get("description_" .. i) bsw@1495: }) bsw@1495: end bsw@1495: end bsw@1495: end bsw@1495: local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. ".json") bsw@1495: local fh = assert(io.open(filename, "w")) bsw@1495: fh:write(json.export(file_uploads)) bsw@1495: fh:write("\n") bsw@1495: fh:close() bsw@1495: end bsw@1495: bsw@1495: local draft_id = Draft:update_content( bsw@1208: app.session.member.id, bsw@1208: param.get("initiative_id", atom.integer), bsw@1208: param.get("formatting_engine"), bsw/jbe@1309: draft_text, bsw@1208: nil, bsw@1208: param.get("preview") or param.get("edit") bsw@1208: ) bsw@1495: bsw@1495: if draft_id and config.attachments then bsw@1495: local file_upload_session = param.get("file_upload_session") bsw@1495: file_upload_session = string.gsub(file_upload_session, "[^A-Za-z0-9]", "") bsw@1495: bsw@1495: local draft_attachments = DraftAttachment:new_selector() bsw@1495: :add_where{ "draft_attachment.draft_id = ?", draft_id } bsw@1495: :exec() bsw@1495: bsw@1495: for i, draft_attachment in ipairs(draft_attachments) do bsw@1495: if param.get("file_delete_" .. draft_attachment.file_id, atom.boolean) then bsw@1495: draft_attachment:destroy() bsw@1495: end bsw@1495: end bsw@1495: bsw@1495: local file_uploads = json.array() bsw@1495: local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. ".json") bsw@1495: local fh = io.open(filename, "r") bsw@1495: if fh then bsw@1495: file_uploads = json.import(fh:read("*a")) bsw@1495: end bsw@1495: for i, file_upload in ipairs(file_uploads) do bsw@1495: local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. "-" .. file_upload.id .. ".jpg") bsw@1495: local data bsw@1495: local fh = io.open(filename, "r") bsw@1495: if fh then bsw@1495: data = fh:read("*a") bsw@1495: end bsw@1495: local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. "-" .. file_upload.id .. ".preview.jpg") bsw@1495: local data_preview bsw@1495: local fh = io.open(filename, "r") bsw@1495: if fh then bsw@1495: data_preview = fh:read("*a") bsw@1495: end bsw@1495: bsw@1495: local hash = moonhash.sha3_512(data) bsw@1495: bsw@1495: local file = File:new_selector() bsw@1495: :add_where{ "hash = ?", hash } bsw@1495: :add_where{ "content_type = ?", "image/jpeg" } bsw@1495: :optional_object_mode() bsw@1495: :exec() bsw@1495: bsw@1495: if not file then bsw@1495: file = File:new() bsw@1495: file.content_type = "image/jpeg" bsw@1495: file.hash = hash bsw@1495: file.data = data bsw@1495: file.preview_content_type = "image/jpeg" bsw@1495: file.preview_data = data_preview bsw@1495: file:save() bsw@1495: end bsw@1495: bsw@1495: local draft_attachment = DraftAttachment:new() bsw@1495: draft_attachment.draft_id = draft_id bsw@1495: draft_attachment.file_id = file.id bsw@1495: draft_attachment.title = file_upload.title bsw@1495: draft_attachment.description = file_upload.description bsw@1495: draft_attachment:save() bsw@1495: end bsw@1495: end bsw@1495: bsw@1495: return draft_id and true or false