local config = {
archiveDir = 'c:/m/notes/monthly',
archiveUrl = '/notes/monthly',
}
local File = require 'filelib'
local Table = require 'extable'
local Numword = require 'numword'
math.randomseed(os.time())
require 'psbind'
local function runshell(shell, cord, cmd)
local results = {}
wv.log('debug','Invoking powershell, cmd=%s',cmd)
cord:cthreaded_multi(
shell:cthread_invoke(cmd),
function( rc ) table.insert(results,rc) end )
wv.log('debug','Powershell invocation returned')
return results
end
local function cordfn_bg_copy( cord )
while true do
collectgarbage()
local msg = cord:getmsg()
local shell = Psbind.Shell() for _, v in ipairs(msg) do
local store = v.archpath
local item = v.item
wv.log('debug','copying "%s" to "%s"', item, store )
runshell( shell, cord, string.format( 'mkdir %s', store ) )
runshell( shell, cord, string.format( 'copy-item "%s" %s/', item, store ) )
end
end
end
local cord_bg_copy = Nylon.cord( 'bgcopy', cordfn_bg_copy )
local RPC = {}
function RPC.archiveFileList(fileList)
local date = os.date( '%y.%m/%d', os.time() )
local rand = Numword.to_s( math.random(6400) + 1376 )
local docopies = {}
local results = Table.map( function(item)
local base = File.leaf(item)
local subdir = date .. '/' .. rand
local store = File.joinpath( config.archiveDir, subdir )
table.insert( docopies, { item = item, archpath = store } )
local url = table.concat( { config.archiveUrl, subdir, base }, '/' )
return url
end, fileList )
cord_bg_copy:msg( docopies )
return results
end
return RPC