add attachement support
This commit is contained in:
parent
8336dc70df
commit
0860ddd4bc
|
@ -1,3 +1,4 @@
|
|||
NOTES
|
||||
credentials.json
|
||||
*.sql
|
||||
public/attachments/
|
||||
|
|
|
@ -40,3 +40,11 @@ div.justify {
|
|||
td, th {
|
||||
vertical-align:top;
|
||||
}
|
||||
.hscroll {
|
||||
width: 100%;
|
||||
overflow-x: auto;
|
||||
white-space: nowrap;
|
||||
}
|
||||
img {
|
||||
height: 200px;
|
||||
}
|
||||
|
|
|
@ -40,6 +40,7 @@
|
|||
<thead><tr><th>property</th><th>value(s)</th></tr></thead>
|
||||
<tbody id="properties"></tbody>
|
||||
</table>
|
||||
<div class="hscroll" id="attachments"></div>
|
||||
<button type="button" onclick="delete_part()">delete part (including children)</button>
|
||||
</div>
|
||||
</td>
|
||||
|
|
|
@ -165,6 +165,19 @@ function select()
|
|||
td.appendChild(input);
|
||||
tr.appendChild(td);
|
||||
properties.appendChild(tr);
|
||||
// add attachments
|
||||
const attachments = document.getElementById('attachments');
|
||||
attachments.innerHTML = null;
|
||||
for (const attachment of part["attachments"]) {
|
||||
const a = document.createElement('a');
|
||||
a.href = attachment;
|
||||
const img = document.createElement('img');
|
||||
img.alt = attachment.split("/").slice(-1);
|
||||
img.src = attachment;
|
||||
a.appendChild(img);
|
||||
attachments.appendChild(a);
|
||||
console.log(attachments);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
12
schema.sql
12
schema.sql
|
@ -65,18 +65,6 @@ CREATE TABLE IF NOT EXISTS distribution (
|
|||
FOREIGN KEY (distributor) REFERENCES distributor (id)
|
||||
);
|
||||
|
||||
-- part attachments
|
||||
CREATE TABLE IF NOT EXISTS attachment (
|
||||
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
|
||||
part INTEGER NOT NULL, -- the part
|
||||
mime TEXT NOT NULL, -- attachment document type
|
||||
path TEXT NOT NULL, -- the path to the attachment
|
||||
priority INTEGER NOT NULL, -- in which place the attachment should be displayed
|
||||
FOREIGN KEY (part) REFERENCES part (id),
|
||||
FOREIGN KEY (type) REFERENCES document (id),
|
||||
UNIQUE (part, priority)
|
||||
);
|
||||
|
||||
-- part property
|
||||
CREATE TABLE IF NOT EXISTS property (
|
||||
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
|
||||
|
|
19
server.rb
19
server.rb
|
@ -13,9 +13,17 @@ require 'mysql2'
|
|||
require 'json'
|
||||
require 'sinatra'
|
||||
|
||||
# allow dumping crashes in browser
|
||||
DEBUG = true
|
||||
# maximum number of parts returned
|
||||
PARTS_LIMIT = 100
|
||||
# credentials for database
|
||||
CREDENTIALS = "credentials.json"
|
||||
# folder name for served pages
|
||||
PUBLIC = "public"
|
||||
# folder name for part attachments (in PUBLIC)
|
||||
ATTACHMENTS = "attachments"
|
||||
|
||||
raise "database information #{CREDENTIALS} do not exist" unless File.file? CREDENTIALS
|
||||
|
||||
# open server
|
||||
|
@ -109,6 +117,17 @@ def get_part_by_id(id)
|
|||
part["properties"][k] ||= v
|
||||
end
|
||||
end
|
||||
# add attachments
|
||||
part["attachments"] = []
|
||||
dir = PUBLIC + "/" + ATTACHMENTS + "/" + part["name"]
|
||||
Dir.entries(dir).each do |file|
|
||||
path = dir + "/" + file
|
||||
next unless File.file? path
|
||||
part["attachments"] << ATTACHMENTS + "/" + part["name"] + "/" + file
|
||||
end
|
||||
if parent then
|
||||
part["attachments"] += parent["attachments"]
|
||||
end
|
||||
# clean up
|
||||
delete = ["parent"]
|
||||
delete.each do |k|
|
||||
|
|
Loading…
Reference in New Issue