add attachement support
This commit is contained in:
parent
8336dc70df
commit
0860ddd4bc
|
@ -1,3 +1,4 @@
|
||||||
NOTES
|
NOTES
|
||||||
credentials.json
|
credentials.json
|
||||||
*.sql
|
*.sql
|
||||||
|
public/attachments/
|
||||||
|
|
|
@ -40,3 +40,11 @@ div.justify {
|
||||||
td, th {
|
td, th {
|
||||||
vertical-align:top;
|
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>
|
<thead><tr><th>property</th><th>value(s)</th></tr></thead>
|
||||||
<tbody id="properties"></tbody>
|
<tbody id="properties"></tbody>
|
||||||
</table>
|
</table>
|
||||||
|
<div class="hscroll" id="attachments"></div>
|
||||||
<button type="button" onclick="delete_part()">delete part (including children)</button>
|
<button type="button" onclick="delete_part()">delete part (including children)</button>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
|
|
|
@ -165,6 +165,19 @@ function select()
|
||||||
td.appendChild(input);
|
td.appendChild(input);
|
||||||
tr.appendChild(td);
|
tr.appendChild(td);
|
||||||
properties.appendChild(tr);
|
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)
|
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
|
-- part property
|
||||||
CREATE TABLE IF NOT EXISTS property (
|
CREATE TABLE IF NOT EXISTS property (
|
||||||
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
|
id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index
|
||||||
|
|
19
server.rb
19
server.rb
|
@ -13,9 +13,17 @@ require 'mysql2'
|
||||||
require 'json'
|
require 'json'
|
||||||
require 'sinatra'
|
require 'sinatra'
|
||||||
|
|
||||||
|
# allow dumping crashes in browser
|
||||||
DEBUG = true
|
DEBUG = true
|
||||||
|
# maximum number of parts returned
|
||||||
PARTS_LIMIT = 100
|
PARTS_LIMIT = 100
|
||||||
|
# credentials for database
|
||||||
CREDENTIALS = "credentials.json"
|
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
|
raise "database information #{CREDENTIALS} do not exist" unless File.file? CREDENTIALS
|
||||||
|
|
||||||
# open server
|
# open server
|
||||||
|
@ -109,6 +117,17 @@ def get_part_by_id(id)
|
||||||
part["properties"][k] ||= v
|
part["properties"][k] ||= v
|
||||||
end
|
end
|
||||||
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
|
# clean up
|
||||||
delete = ["parent"]
|
delete = ["parent"]
|
||||||
delete.each do |k|
|
delete.each do |k|
|
||||||
|
|
Loading…
Reference in New Issue