-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomExportFilter.lua
More file actions
200 lines (159 loc) · 6.29 KB
/
CustomExportFilter.lua
File metadata and controls
200 lines (159 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
--[[============================================================================
CustomExportFilter.lua
Export filter — injects custom XMP metadata
into exported files (JPG/TIFF) using ExifTool.
Filter API:
- for rendition in filterContext:renditions{ stopIfCanceled = true }
- rendition:waitForRender() → success, path
- rendition.photo → LrPhoto
============================================================================--]]
local LrDialogs = import "LrDialogs"
local LrFileUtils = import "LrFileUtils"
local LrPathUtils = import "LrPathUtils"
local LrTasks = import "LrTasks"
local LrView = import "LrView"
local LrStringUtils = import "LrStringUtils"
local SC = require "SharedConfig"
local Log = require "PluginLogger"
local logger = Log.getLogger("ExportFilter")
--------------------------------------------------------------------------
-- Helpers
--------------------------------------------------------------------------
local function isExifToolAvailable()
local cmd = WIN_ENV and "where exiftool >NUL 2>&1"
or "which exiftool >/dev/null 2>&1"
return LrTasks.execute(cmd) == 0
end
--------------------------------------------------------------------------
-- Argfile and ExifTool
--------------------------------------------------------------------------
local function writeArgFile(tmpDir, idx, photo, filePath)
local argPath = LrPathUtils.child(
tmpDir, "custmeta_e_" .. tostring(idx) .. ".txt")
local lines = {}
if WIN_ENV then
table.insert(lines, "-charset")
table.insert(lines, "filename=utf8")
end
local hasData = false
for _, field in ipairs(SC.FIELD_MAP) do
local value = photo:getPropertyForPlugin(SC.PLUGIN_ID, field.id)
if value then
local trimmed = LrStringUtils.trimWhitespace(tostring(value))
if trimmed ~= "" then
hasData = true
table.insert(lines, string.format(
"-XMP-%s:%s=%s", SC.XMP_PREFIX, field.xmpTag, trimmed))
end
end
end
if not hasData then return nil end
table.insert(lines, "-overwrite_original")
table.insert(lines, filePath)
local f = io.open(argPath, "wb")
if not f then return nil end
f:write(table.concat(lines, "\n") .. "\n")
f:close()
return argPath
end
local function runExifTool(configPath, argPath, tmpDir, idx)
local outPath = LrPathUtils.child(tmpDir, "custmeta_e_out_" .. tostring(idx) .. ".txt")
local cmd = string.format('exiftool -config "%s" -@ "%s" > "%s" 2>&1',
configPath, argPath, outPath)
if Log.isEnabled("trace") then logger:trace("CMD: " .. cmd) end
local exitCode = LrTasks.execute(cmd)
if Log.isEnabled("trace") then
logger:trace("Exit: " .. tostring(exitCode))
if exitCode ~= 0 then
logger:trace("Output: " .. SC.readFileContents(outPath))
end
end
SC.safeDelete(outPath)
return exitCode == 0
end
--------------------------------------------------------------------------
-- UI Section
--------------------------------------------------------------------------
local function sectionForFilterInDialog(viewFactory, propertyTable)
return {
title = "Custom XMP Metadata",
viewFactory:row {
viewFactory:static_text {
title = "Injects custom metadata from the catalog\n"
.. "into XMP embedded in the exported file.\n\n"
.. "Requires ExifTool (exiftool.org).",
fill_horizontal = 1,
},
},
viewFactory:row {
viewFactory:checkbox {
title = "Inject custom XMP metadata",
value = LrView.bind("embedCustomXMP"),
},
},
}
end
--------------------------------------------------------------------------
-- Post-process
--------------------------------------------------------------------------
local function postProcessRenderedPhotos(functionContext, filterContext)
if not filterContext then return end
local propertyTable = filterContext.propertyTable
if not (propertyTable and propertyTable.embedCustomXMP) then
for rendition in filterContext:renditions{ stopIfCanceled = true } do
rendition:waitForRender()
end
return
end
if not isExifToolAvailable() then
LrDialogs.showError("ExifTool not found in PATH.\n"
.. "Install from https://exiftool.org")
for rendition in filterContext:renditions{ stopIfCanceled = true } do
rendition:waitForRender()
end
return
end
local tmpDir = LrPathUtils.getStandardFilePath("temp")
local configPath = SC.writeExifToolConfig(tmpDir, "custmeta_e.config")
if not configPath then
for rendition in filterContext:renditions{ stopIfCanceled = true } do
rendition:waitForRender()
end
return
end
local cleanUp = {}
local embedCount = 0
local totalCount = 0
for rendition in filterContext:renditions{ stopIfCanceled = true } do
totalCount = totalCount + 1
local success, pathOrMsg = rendition:waitForRender()
if success then
local argPath = writeArgFile(tmpDir, totalCount, rendition.photo, pathOrMsg)
if argPath then
table.insert(cleanUp, argPath)
if runExifTool(configPath, argPath, tmpDir, totalCount) then
embedCount = embedCount + 1
else
logger:warn("ExifTool failed: " .. pathOrMsg)
end
end
else
logger:warn("Render failed: " .. tostring(pathOrMsg))
end
end
if Log.isEnabled("trace") then
logger:trace("Embedded: " .. embedCount .. "/" .. totalCount)
end
for _, p in ipairs(cleanUp) do SC.safeDelete(p) end
SC.safeDelete(configPath)
end
--------------------------------------------------------------------------
-- Export Filter Provider
--------------------------------------------------------------------------
return {
exportPresetFields = {
{ key = "embedCustomXMP", default = true },
},
sectionForFilterInDialog = sectionForFilterInDialog,
postProcessRenderedPhotos = postProcessRenderedPhotos,
}