+
diff --git a/public/partdb.js b/public/partdb.js
index 49a6ea4..b11298e 100644
--- a/public/partdb.js
+++ b/public/partdb.js
@@ -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);
+ }
}
}
}
diff --git a/schema.sql b/schema.sql
index 45c64bb..dfaba99 100644
--- a/schema.sql
+++ b/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
diff --git a/server.rb b/server.rb
index 62914f9..3e1025b 100755
--- a/server.rb
+++ b/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|