2010-11-13 19:22:17 +00:00
|
|
|
----
|
2010-11-14 09:38:44 +00:00
|
|
|
-- Lsyncd user-script that creates a "magic" image converter directory.
|
2012-09-07 06:58:44 +00:00
|
|
|
--
|
2010-11-14 09:38:44 +00:00
|
|
|
-- This configuration will automatically convert all images that are placed
|
2010-11-13 19:22:17 +00:00
|
|
|
-- in the directory 'magicdir' all resulting images are placed in the same
|
|
|
|
-- directory!
|
|
|
|
--
|
|
|
|
-- Be sure to mkdir 'magicdir' first.
|
|
|
|
|
2010-11-14 09:38:44 +00:00
|
|
|
-----
|
|
|
|
-- Fileformats: .jpg .gif .png
|
|
|
|
--
|
2010-11-13 19:22:17 +00:00
|
|
|
local formats = { jpg=true, gif=true, png=true, }
|
|
|
|
|
2022-11-11 14:27:21 +00:00
|
|
|
local convert = {
|
2010-11-13 19:22:17 +00:00
|
|
|
delay = 0,
|
|
|
|
|
|
|
|
maxProcesses = 99,
|
|
|
|
|
|
|
|
action = function(inlet)
|
|
|
|
local event = inlet.getEvent()
|
|
|
|
|
|
|
|
if event.isdir then
|
|
|
|
-- ignores events on dirs
|
|
|
|
inlet.discardEvent(event)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- extract extension and basefilename
|
|
|
|
local p = event.pathname
|
|
|
|
local ext = string.match(p, ".*%.([^.]+)$")
|
|
|
|
local base = string.match(p, "(.*)%.[^.]+$")
|
|
|
|
if not formats[ext] then
|
|
|
|
-- an unknown extenion
|
|
|
|
log("Normal", "not doing something on ."..ext)
|
|
|
|
inlet.discardEvent(event)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- autoconvert on create and modify
|
|
|
|
if event.etype == "Create" or event.etype == "Modify" then
|
|
|
|
-- builds one bash command
|
|
|
|
local cmd = ""
|
|
|
|
-- do for all other extensions
|
|
|
|
for k, _ in pairs(formats) do
|
|
|
|
if k ~= ext then
|
|
|
|
-- excludes files to be created, so no
|
|
|
|
-- followup actions will occur
|
|
|
|
inlet.addExclude(base..'.'..k)
|
|
|
|
if cmd ~= "" then
|
|
|
|
cmd = cmd .. " && "
|
|
|
|
end
|
2012-09-07 06:58:44 +00:00
|
|
|
cmd = cmd..
|
2010-11-13 19:22:17 +00:00
|
|
|
'/usr/bin/convert "'..
|
|
|
|
event.source..p..'" "'..
|
|
|
|
event.source..base..'.'..k..
|
|
|
|
'" || /bin/true'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
log("Normal", "Converting "..p)
|
|
|
|
spawnShell(event, cmd)
|
|
|
|
return
|
|
|
|
end
|
2012-09-07 06:58:44 +00:00
|
|
|
|
2010-11-13 19:22:17 +00:00
|
|
|
-- deletes all formats if you delete one
|
|
|
|
if event.etype == "Delete" then
|
|
|
|
-- builds one bash command
|
|
|
|
local cmd = ""
|
|
|
|
-- do for all other extensions
|
|
|
|
for k, _ in pairs(formats) do
|
|
|
|
if k ~= ext then
|
|
|
|
-- excludes files to be created, so no
|
|
|
|
-- followup actions will occur
|
|
|
|
inlet.addExclude(base..'.'..k)
|
|
|
|
if cmd ~= "" then
|
|
|
|
cmd = cmd .. " && "
|
|
|
|
end
|
|
|
|
cmd = cmd..
|
|
|
|
'rm "'..event.source..base..'.'..k..
|
|
|
|
'" || /bin/true'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
log("Normal", "Deleting all "..p)
|
|
|
|
spawnShell(event, cmd)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- ignores other events.
|
|
|
|
inlet.discardEvent(event)
|
|
|
|
end,
|
|
|
|
|
|
|
|
-----
|
|
|
|
-- Removes excludes when convertions are finished
|
|
|
|
--
|
|
|
|
collect = function(event, exitcode)
|
|
|
|
local p = event.pathname
|
|
|
|
local ext = string.match(p, ".*%.([^.]+)$")
|
|
|
|
local base = string.match(p, "(.*)%.[^.]+$")
|
|
|
|
local inlet = event.inlet
|
|
|
|
|
|
|
|
if event.etype == "Create" or
|
2012-09-07 06:58:44 +00:00
|
|
|
event.etype == "Modify" or
|
|
|
|
event.etype == "Delete"
|
2010-11-13 19:22:17 +00:00
|
|
|
then
|
|
|
|
for k, _ in pairs(formats) do
|
|
|
|
inlet.rmExclude(base..'.'..k)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
-----
|
|
|
|
-- Does not collapse anything
|
|
|
|
collapse = function()
|
|
|
|
return 3
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
2010-11-20 13:10:52 +00:00
|
|
|
sync{convert, source="magicdir", subdirs=false}
|
2010-11-13 19:22:17 +00:00
|
|
|
|