--[[ Job processing for the Lichtkasten Bridge. Job file format (one key=value per line, "file=" may repeat): id, preset, collection, export (0/1), exportFolder, quality (0-100), longEdge (px, 0 = full), sharpening (none|screen|matte|glossy), colorSpace (sRGB|AdobeRGB), file=/abs/path ... ]] local LrApplication = import 'LrApplication' local LrExportSession = import 'LrExportSession' local LrFileUtils = import 'LrFileUtils' local LrPathUtils = import 'LrPathUtils' local LrLogger = import 'LrLogger' local LrTasks = import 'LrTasks' local log = LrLogger('LichtkastenBridge') local Bridge = {} local function root() local appData = LrPathUtils.getStandardFilePath('appData') return LrPathUtils.child(LrPathUtils.child(appData, 'Lichtkasten'), 'LRBridge') end function Bridge.inbox() return LrPathUtils.child(root(), 'inbox') end function Bridge.outbox() return LrPathUtils.child(root(), 'outbox') end local function ensureDir(path) if not LrFileUtils.exists(path) then LrFileUtils.createAllDirectories(path) end end function Bridge.heartbeat() ensureDir(root()) local f = io.open(LrPathUtils.child(root(), 'heartbeat'), 'w') if f then f:write(tostring(os.time())); f:close() end end local function parseJob(path) local job = { files = {}, quality = 90, longEdge = 0, export = false, preset = '', collection = '', sharpening = 'none', colorSpace = 'sRGB', exportFolder = '' } local f = io.open(path, 'r') if not f then return nil end for line in f:lines() do local k, v = string.match(line, '^([^=]+)=(.*)$') if k == 'file' then if v ~= '' then table.insert(job.files, v) end elseif k == 'export' then job.export = (v == '1') elseif k == 'quality' or k == 'longEdge' then job[k] = tonumber(v) or job[k] elseif k then job[k] = v end end f:close() return job end local function findPreset(name) if name == nil or name == '' then return nil end -- "Ordner/Preset" or just "Preset" local folderName, presetName = string.match(name, '^(.-)/([^/]+)$') presetName = presetName or name for _, folder in ipairs(LrApplication.developPresetFolders()) do if folderName == nil or folder:getName() == folderName then for _, preset in ipairs(folder:getDevelopPresets()) do if preset:getName() == presetName then return preset end end end end return nil end local function writeResult(job, status, imported, exported, errors) local outbox = Bridge.outbox() ensureDir(outbox) local tmp = LrPathUtils.child(outbox, job.id .. '.tmp') local f = io.open(tmp, 'w') if not f then return end f:write('id=' .. job.id .. '\n') f:write('status=' .. status .. '\n') f:write('imported=' .. tostring(imported) .. '\n') for _, p in ipairs(exported) do f:write('exported=' .. p .. '\n') end for _, e in ipairs(errors) do f:write('error=' .. string.gsub(tostring(e), '\n', ' ') .. '\n') end f:close() os.rename(tmp, LrPathUtils.child(outbox, job.id .. '.result')) end local function exportPhotos(job, photos, exported, errors) ensureDir(job.exportFolder) local constrain = (job.longEdge or 0) > 0 local settings = { LR_export_destinationType = 'specificFolder', LR_export_destinationPathPrefix = job.exportFolder, LR_export_useSubfolder = false, LR_collisionHandling = 'rename', LR_format = 'JPEG', LR_jpeg_quality = (job.quality or 90) / 100, LR_jpeg_useLimitSize = false, LR_export_colorSpace = job.colorSpace or 'sRGB', LR_export_bitDepth = 8, LR_size_doConstrain = constrain, LR_size_doNotEnlarge = true, LR_size_resizeType = 'longEdge', LR_size_maxHeight = constrain and job.longEdge or 0, LR_size_maxWidth = constrain and job.longEdge or 0, LR_size_units = 'pixels', LR_size_resolution = 300, LR_size_resolutionUnits = 'inch', LR_outputSharpeningOn = job.sharpening ~= 'none', LR_outputSharpeningMedia = (job.sharpening ~= 'none') and job.sharpening or 'screen', LR_outputSharpeningLevel = 2, LR_minimizeEmbeddedMetadata = false, LR_metadata_keywordOptions = 'flat', LR_removeLocationMetadata = false, LR_removeFaceMetadata = false, LR_includeVideoFiles = false, LR_reimportExportedPhoto = false, LR_renamingTokensOn = false, LR_useWatermark = false, } local session = LrExportSession({ photosToExport = photos, exportSettings = settings }) session:doExportOnCurrentTask() for _, rendition in session:renditions() do local ok, pathOrMessage = rendition:waitForRender() if ok then table.insert(exported, pathOrMessage) else table.insert(errors, 'Export: ' .. tostring(pathOrMessage)) end end end local function processJob(job) local catalog = LrApplication.activeCatalog() local photos, errors, exported = {}, {}, {} catalog:withWriteAccessDo('Lichtkasten: Import', function() for _, path in ipairs(job.files) do if LrFileUtils.exists(path) then local photo = catalog:findPhotoByPath(path) if not photo then local ok, result = LrTasks.pcall(function() return catalog:addPhoto(path) end) if ok then photo = result else table.insert(errors, path .. ': ' .. tostring(result)) end end if photo then table.insert(photos, photo) end else table.insert(errors, 'Datei fehlt: ' .. path) end end end, { timeout = 60 }) if #photos > 0 then local preset = findPreset(job.preset) if job.preset ~= '' and preset == nil then table.insert(errors, 'Entwicklungsvorgabe nicht gefunden: ' .. job.preset) end catalog:withWriteAccessDo('Lichtkasten: Vorgabe & Sammlung', function() if preset then for _, photo in ipairs(photos) do photo:applyDevelopPreset(preset) end end if job.collection ~= '' then local collection = catalog:createCollection(job.collection, nil, true) if collection then collection:addPhotos(photos) end end end, { timeout = 60 }) if job.export and job.exportFolder ~= '' then exportPhotos(job, photos, exported, errors) end -- Select the imported photos so the user sees them right away. pcall(function() catalog:setSelectedPhotos(photos[1], photos) end) end local status = (#errors == 0 or #photos > 0) and 'done' or 'error' writeResult(job, status, #photos, exported, errors) log:info(string.format('Job %s: %d importiert, %d exportiert, %d Fehler', job.id, #photos, #exported, #errors)) end --- Processes all pending jobs. Returns the number of processed jobs. function Bridge.processInbox() local inbox = Bridge.inbox() ensureDir(inbox) local count = 0 local pending = {} for path in LrFileUtils.files(inbox) do if LrPathUtils.extension(path) == 'job' then table.insert(pending, path) end end table.sort(pending) for _, path in ipairs(pending) do do local working = LrPathUtils.replaceExtension(path, 'working') if os.rename(path, working) then local job = parseJob(working) if job and job.id then processJob(job) count = count + 1 end LrFileUtils.delete(working) end end end return count end return Bridge