|
@@ -2,7 +2,6 @@ local io = io
|
|
|
|
|
|
local csv = require "csv"
|
|
local csv = require "csv"
|
|
local dir = require "pl.dir"
|
|
local dir = require "pl.dir"
|
|
-local uuid = require "uuid"
|
|
|
|
local plpath = require "pl.path"
|
|
local plpath = require "pl.path"
|
|
|
|
|
|
local term = require "lsup.term"
|
|
local term = require "lsup.term"
|
|
@@ -12,12 +11,6 @@ local graph = require "lsup.graph"
|
|
local mc = require "pocket_archive.monocypher"
|
|
local mc = require "pocket_archive.monocypher"
|
|
local pkar = require "pocket_archive"
|
|
local pkar = require "pocket_archive"
|
|
|
|
|
|
--- Random number generator for uuid()
|
|
|
|
-local posix_uuid = pcall(function()
|
|
|
|
- uuid.set_rng(uuid.rng.urandom())
|
|
|
|
-end)
|
|
|
|
--- FIXME
|
|
|
|
-if not posix_uuid then rng = uuid.set_rng(uuid.rng.win_ffi()) end
|
|
|
|
|
|
|
|
local M = {} -- Submission module
|
|
local M = {} -- Submission module
|
|
|
|
|
|
@@ -43,6 +36,28 @@ local function escape_pattern(s)
|
|
end
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
+-- For idgen(). Makes a 60-character pool with ~5.9 bits of entropy per char.
|
|
|
|
+local chpool = {}
|
|
|
|
+for i = 48, 57 do table.insert(chpool, i) end -- 0-9
|
|
|
|
+for i = 65, 90 do table.insert(chpool, i) end -- A-Z
|
|
|
|
+for i = 97, 122 do table.insert(chpool, i) end -- a-z
|
|
|
|
+
|
|
|
|
+--[[
|
|
|
|
+Generate a random, reader-friendly ID.
|
|
|
|
+
|
|
|
|
+A 16-character ID with the above defined #chpool of 60 smybols has an entropy
|
|
|
|
+of 94.5 bits, which should be plenty for a small repository.
|
|
|
|
+]]
|
|
|
|
+M.idgen = function(len)
|
|
|
|
+ local charlist = {}
|
|
|
|
+ for i = 1, (len or pkar.config.id.len) do
|
|
|
|
+ table.insert(charlist, string.char(chpool[math.random(1, #chpool)]))
|
|
|
|
+ end
|
|
|
|
+
|
|
|
|
+ return table.concat(charlist)
|
|
|
|
+end
|
|
|
|
+
|
|
|
|
+
|
|
--[=[
|
|
--[=[
|
|
M.generate_sip_v1 = function(path)
|
|
M.generate_sip_v1 = function(path)
|
|
--[[
|
|
--[[
|
|
@@ -104,7 +119,7 @@ M.generate_sip_v1 = function(path)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
- md[ref] = md[ref] or {id = uuid(), path = ref, _sort = i}
|
|
|
|
|
|
+ md[ref] = md[ref] or {id = M.idgen(), path = ref, _sort = i}
|
|
md[ref][k] = md[ref][k] or {}
|
|
md[ref][k] = md[ref][k] or {}
|
|
if k == "type" then
|
|
if k == "type" then
|
|
md[ref][k] = v
|
|
md[ref][k] = v
|
|
@@ -157,7 +172,7 @@ M.generate_sip_v2 = function(path)
|
|
if row["path"] ~= "" then
|
|
if row["path"] ~= "" then
|
|
prev_path = row["path"]
|
|
prev_path = row["path"]
|
|
-- New row.
|
|
-- New row.
|
|
- sip[i] = {id = uuid()}
|
|
|
|
|
|
+ sip[i] = {id = M.idgen()}
|
|
for k, v in pairs(row) do
|
|
for k, v in pairs(row) do
|
|
if v == "" then goto cont1 end -- skip empty strings.
|
|
if v == "" then goto cont1 end -- skip empty strings.
|
|
if pkar.config.md.single_values[k] then sip[i][k] = v
|
|
if pkar.config.md.single_values[k] then sip[i][k] = v
|
|
@@ -218,12 +233,13 @@ end
|
|
|
|
|
|
M.update_rsrc_md = function(rsrc)
|
|
M.update_rsrc_md = function(rsrc)
|
|
-- TODO use a transaction when lsup_lua supports it.
|
|
-- TODO use a transaction when lsup_lua supports it.
|
|
- gr = graph.new(pkar.store, "par:" .. rsrc.id)
|
|
|
|
- rsrc.id = nil -- Exclude from metadata scan.
|
|
|
|
-
|
|
|
|
triples = {}
|
|
triples = {}
|
|
- local s = term.new_iriref("")
|
|
|
|
|
|
+
|
|
|
|
+ local s = term.new_iriref("par:" .. rsrc.id, pkar.nsm)
|
|
|
|
+ gr = graph.new(pkar.store, s.data, pkar.nsm)
|
|
|
|
+ rsrc.id = nil -- Exclude from metadata scan.
|
|
for k, v in pairs(rsrc) do
|
|
for k, v in pairs(rsrc) do
|
|
|
|
+ print("Adding attribute:", k, v)
|
|
local p = term.new_iriref(k, pkar.nsm)
|
|
local p = term.new_iriref(k, pkar.nsm)
|
|
if type(v) == "table" then
|
|
if type(v) == "table" then
|
|
for vv, _ in pairs(v) do
|
|
for vv, _ in pairs(v) do
|
|
@@ -235,6 +251,8 @@ M.update_rsrc_md = function(rsrc)
|
|
print("Removing triples.")
|
|
print("Removing triples.")
|
|
gr:remove();
|
|
gr:remove();
|
|
print("Adding triples.")
|
|
print("Adding triples.")
|
|
|
|
+ -- TODO implement lsup_lua fn to add a single triple and add triples in
|
|
|
|
+ -- the previous loop.
|
|
gr:add(triples)
|
|
gr:add(triples)
|
|
end
|
|
end
|
|
|
|
|
|
@@ -284,7 +302,7 @@ M.deposit = function(sip)
|
|
out_path = out_dir .. checksum:sub(1,32)
|
|
out_path = out_dir .. checksum:sub(1,32)
|
|
rsrc.path = out_path
|
|
rsrc.path = out_path
|
|
dir.makepath(out_dir)
|
|
dir.makepath(out_dir)
|
|
- print(("Moving file %s t %s"):format(tmp_path, rsrc.path))
|
|
|
|
|
|
+ print(("Moving file %s to %s"):format(tmp_path, rsrc.path))
|
|
dir.movefile(tmp_path, rsrc.path)
|
|
dir.movefile(tmp_path, rsrc.path)
|
|
end
|
|
end
|
|
|
|
|