-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharedConfig.lua
More file actions
157 lines (129 loc) · 4.58 KB
/
SharedConfig.lua
File metadata and controls
157 lines (129 loc) · 4.58 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
--[[============================================================================
SharedConfig.lua
Module shared by all plugin scripts.
Dynamically generates ExifTool configuration based on FieldDefinitions.
============================================================================--]]
local LrFileUtils = import "LrFileUtils"
local LrPathUtils = import "LrPathUtils"
local Config = require "PluginConfig"
local Fields = require "FieldDefinitions"
local Log = require "PluginLogger"
local logger = Log.getLogger("SharedConfig")
local SC = {}
-- Re-export constants from Config (convenience)
SC.PLUGIN_ID = Config.PLUGIN_ID
SC.XMP_NS = Config.XMP_NS
SC.XMP_PREFIX = Config.XMP_PREFIX
SC.FIELD_MAP = Fields.FIELDS
--------------------------------------------------------------------------
-- ExifTool config generation from FieldDefinitions
--------------------------------------------------------------------------
function SC.writeExifToolConfig(tmpDir, configName)
configName = configName or "custom_xmp.config"
local configPath = LrPathUtils.child(tmpDir, configName)
-- Header: register namespace in XMP::Main
local lines = {
"# Auto-generated ExifTool config from FieldDefinitions.lua",
"%Image::ExifTool::UserDefined = (",
" 'Image::ExifTool::XMP::Main' => {",
" " .. SC.XMP_PREFIX .. " => {",
" SubDirectory => {",
" TagTable => 'Image::ExifTool::UserDefined::"
.. SC.XMP_PREFIX .. "',",
" },",
" },",
" },",
");",
"",
"# Tag definitions",
"%Image::ExifTool::UserDefined::" .. SC.XMP_PREFIX .. " = (",
" GROUPS => { 0 => 'XMP', 1 => 'XMP-" .. SC.XMP_PREFIX
.. "', 2 => 'Other' },",
" NAMESPACE => { '" .. SC.XMP_PREFIX .. "' => '"
.. SC.XMP_NS .. "' },",
" WRITABLE => 'string',",
}
-- Dynamic tag list from FieldDefinitions
for _, field in ipairs(Fields.FIELDS) do
table.insert(lines, " " .. field.xmpTag .. " => { },")
end
table.insert(lines, ");")
table.insert(lines, "1;")
table.insert(lines, "")
local content = table.concat(lines, "\n")
local f = io.open(configPath, "w")
if not f then
logger:error("Cannot create config: " .. configPath)
return nil
end
f:write(content)
f:close()
if Log.isEnabled("trace") then
logger:trace("ExifTool config written: " .. configPath)
end
return configPath
end
--------------------------------------------------------------------------
-- Temporary files
--------------------------------------------------------------------------
function SC.safeDelete(path)
if path and LrFileUtils.exists(path) then
LrFileUtils.delete(path)
end
end
function SC.readFileContents(path)
if not path or not LrFileUtils.exists(path) then
return "(file does not exist)"
end
local f = io.open(path, "r")
if not f then return "(cannot open)" end
local content = f:read("*all")
f:close()
return content or "(empty)"
end
--------------------------------------------------------------------------
-- RAW / sidecar
--------------------------------------------------------------------------
function SC.isRawFile(filePath)
local ext = LrPathUtils.extension(filePath)
if not ext then return false end
return Config.RAW_EXTENSIONS[string.lower(ext)] == true
end
function SC.getSidecarPath(filePath)
local dir = LrPathUtils.parent(filePath)
local base = LrPathUtils.removeExtension(LrPathUtils.leafName(filePath))
return LrPathUtils.child(dir, base .. ".xmp")
end
function SC.ensureSidecarExists(sidecarPath)
if LrFileUtils.exists(sidecarPath) then
return true
end
local minXmp = [[<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about=""/>
</rdf:RDF>
</x:xmpmeta>
<?xpacket end="w"?>
]]
local f = io.open(sidecarPath, "wb")
if not f then
logger:warn("Cannot create sidecar: " .. sidecarPath)
return false
end
f:write(minXmp)
f:close()
if Log.isEnabled("trace") then
logger:trace("Created sidecar: " .. sidecarPath)
end
return true
end
function SC.getTargetPath(filePath)
if SC.isRawFile(filePath) then
local sidecarPath = SC.getSidecarPath(filePath)
SC.ensureSidecarExists(sidecarPath)
return sidecarPath, true
end
return filePath, false
end
return SC