liquid_feedback_frontend

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

Impressum / About Us