-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck for Possible Duplicate Media.fh_lua
374 lines (358 loc) · 13.2 KB
/
Check for Possible Duplicate Media.fh_lua
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
--[[
@Title: Check for Possible Duplicate Media
@Author: Jane Taubman
@Version: 1.3
@LastUpdated: June 2013
@Description:
This plugin checks all the media records in your project and compares the files linked to them looking for files
which are identical it will pick up both records which are linked to the same file and to those where the names
are different, but the contents are the same.
When complete it returns a result set containing the matching records.
By selecting each pair in turn, just drag over both cells, you can then select Edit>Merge Compare Records from
the menu and merge the records together.
Once the process is complete, it is recommended to use the "Check for Unlinked Media" plugin to clean up any duplicate files.
1.1 Add trap for Missing Files and report them on the Result Set.
1.2 Add Caching of File details between runs
1.3 Correct Clear Records Call
]]
function mainfunction()
if fhGetContextInfo('CI_APP_MODE') ~= 'Project Mode' then
fhMessageBox('This Plugin Requires a Project','MB_OK','MB_ICONEXCLAMATION')
return
end
-- Get Previous Run Data / Set up Database
local dbenv,dbcon,bCreate = opendb(fhGetPluginDataFileName())
iCount = countrecords(dbcon)
if iCount and iCount > 0 then
local ans = fhMessageBox('There are '..iCount..' Cached Records..\nDo you wish to use them?','MB_YESNO','MB_ICONQUESTION')
if ans == 'No' then clearlist(dbcon) -- was clearrecords() in V1.2
elseif ans ~= 'Yes' then return end
end
-- Define Columns
tblOutput,iC = createResultTable()
tblOutput.record = {title='Record',type='item',width=140,align='align_left',content={}}
tblOutput.duplicate = {title='Duplicate',type='item',width=140,align='align_left',content={}}
tblOutput.message = {title='Message',type='Text',width=140,align='align_left',content={}}
-- Build Duplicate List
local rootfolder = fhGetContextInfo('CI_PROJECT_DATA_FOLDER')
local medialist = {}
local filelist = {}
local mediaCount = 0
local pm = fhNewItemPtr()
-- Quick Count Records
local icnt = 0
for pi in records('OBJE') do
icnt = icnt + 1
end
ProgressDisplay.Start('Searching All Media Please Wait',icnt)
for pi in records('OBJE') do
local recordid = fhGetRecordId(pi)
ProgressDisplay.SetMessage(fhGetDisplayText(pi))
ProgressDisplay.Step(1)
if ProgressDisplay.Cancel() then
break
end
pm:MoveTo(pi,'~._FILE')
local mediaFile = fhGetValueAsText(pm)
if mediaFile:starts('Media\\') then
mediaFile = rootfolder..'\\'..mediaFile
end
local strlc = mediaFile:lower()
if FileExists(mediaFile) then
local smd5 = chkrecord(dbcon,recordid)
if not(sdm5) then
smd5 = md5.sumhexa(LoadFromFile(mediaFile))
updaterecord(dbcon,recordid,smd5)
end
local newmedia = {filename = fhGetValueAsText(pm), record = pi:Clone(), md5 = smd5}
-- Check for Duplicate
bDup = false
for i = 1,mediaCount do
if smd5 == medialist[i].md5 then
iC = iC + 1
tblOutput.record.content[iC] = medialist[i].record
tblOutput.duplicate.content[iC] = newmedia.record
bDup = true
end
end
if not(bDup) then
mediaCount = mediaCount + 1
medialist[mediaCount] = newmedia
end
else
-- File not found
iC = iC + 1
tblOutput.record.content[iC] = pi:Clone()
tblOutput.message.content[iC] = 'File Missing:'..fhGetValueAsText(pm)
end
end
ProgressDisplay.Reset()
ProgressDisplay.Close()
if iC > 0 then
-- Offer to delete the files
local strTitle = "Possible Duplicate Media Records"
fhOutputResultSetTitles(strTitle,strTitle, "Date: %#x")
for t in tblOutput() do
fhOutputResultSetColumn(t.title, t.type, t.content, iC, t.width,t.align)
end
else
fhMessageBox('No Duplicates Found','MB_OK','MB_ICONINFORMATION')
end
end
-------------------------------------- Custom Functions
-------------------------------------- Standard Functions
--[[
@Title: Progress Display (drop in)
@Author: Jane Taubman / Mike Tate
@LastUpdated: May 2012
@Description: Allows easy adding of a Progress Bar to any long running Plugin
]]
local StrWhite = "255 255 255"
ProgressDisplay = {
Start = function(strTitle,intMax) -- Create and start the Progress Display window controls
if not dlgProgress then
cancelflag = false
local cancelbutton = iup.button{ title="Cancel", rastersize="200x30",
action = function()
cancelflag = true -- Signal that Cancel button was pressed
return iup.CLOSE
end
}
gaugeProgress = iup.progressbar{ rastersize="400x30", max=intMax } -- Set progress bar maximum range
messageline = iup.label{ title=" ", expand="YES", alignment="ACENTER" }
dlgProgress = iup.dialog{ title=strTitle, dialogframe="YES", background=StrWhite, -- Remove Windows minimize/maximize menu
iup.vbox{ alignment="ACENTER", gap="10", margin="10x10",
messageline,
gaugeProgress,
cancelbutton
}
}
dlgProgress.close_cb = cancelbutton.action -- Windows Close button acts as Cancel button
dlgProgress:showxy(iup.CENTER, iup.CENTER) -- Show the Progress Display dialogue window
end
end,
SetMessage = function(strMessage) -- Set the progress message
if dlgProgress then messageline.title = strMessage end
end,
Step = function(iStep) -- Step the Progress Bar forward
if dlgProgress then
gaugeProgress.value = gaugeProgress.value + iStep
local val = tonumber(gaugeProgress.value)
local max = tonumber(gaugeProgress.max)
if val > max then
gaugeProgress.value = 0
end
iup.LoopStep()
end
end,
Reset = function() -- Reset progress bar
if dlgProgress then gaugeProgress.value = 0 end
end,
Cancel = function() -- Check if Cancel button pressed
return cancelflag
end,
Close = function() -- Close the dialogue window
cancelflag = false
if dlgProgress then dlgProgress:destroy() dlgProgress = nil end
end,
}
function records(type)
local pi = fhNewItemPtr()
local p2 = fhNewItemPtr()
pi:MoveToFirstRecord(type)
return function ()
p2:MoveTo(pi)
pi:MoveNext()
if p2:IsNotNull() then return p2 end
end
end
function createResultTable()
-- create metatable
local tblOutput_mt = {}
tblOutput_mt.col = 0
tblOutput_mt.seq = {}
tblOutput_mt.__newindex = function (t,k,v)
rawset(t,k,v) -- update original table
local m = getmetatable(t)
m.col = m.col + 1
table.insert(m.seq,k)
end
tblOutput_mt.__call = function (t)
local i = 0
local m = getmetatable(t)
local n = table.getn(m.seq)
return function ()
i = i + 1
if i <= n then return t[m.seq[i]] end
end
end
local tblOutput = {} -- Define Columns Table
setmetatable(tblOutput, tblOutput_mt)
local iC = 0 -- Define count of lines
return tblOutput,iC
end
function loadrequire(module,extended)
if not(extended) then extended = module end
local function installmodule(module,filename)
local bmodule = false
if not(filename) then
filename = module..'.mod'
bmodule = true
end
local storein = fhGetContextInfo('CI_APP_DATA_FOLDER')..'\\Plugins\\'
-- Check if subdirectory needed
local path = string.match(filename, "(.-)[^/]-[^%.]+$")
if path ~= "" then
path = path:gsub('/','\\')
-- Create sub-directory
lfs.mkdir(storein..path)
end
-- Get file down and install it
local http = luacom.CreateObject("winhttp.winhttprequest.5.1")
local url = "http://www.family-historian.co.uk/lnk/getpluginmodule.php?file="..filename
http:Open("GET",url,false)
http:Send()
http:WaitForResponse(30)
local status = http.StatusText
if status == 'OK' then
length = http:GetResponseHeader('Content-Length')
data = http.ResponseBody
if bmodule then
local modlist = loadstring(http.ResponseBody)
for _,f in pairs(modlist()) do
if not(installmodule(module,f)) then
break
end
end
else
local function OpenFile(strFileName,strMode)
local fileHandle, strError = io.open(strFileName,strMode)
if not fileHandle then
error("\n Unable to open file in \""..strMode.."\" mode. \n "..
strFileName.." \n "..tostring(strError).." \n")
end
return fileHandle
end -- OpenFile
local function SaveStringToFile(strString,strFileName)
local fileHandle = OpenFile(strFileName,"wb")
fileHandle:write(strString)
assert(fileHandle:close())
end -- SaveStringToFile
SaveStringToFile(data,storein..filename)
end
return true
else
fhMessageBox('An error occurred in Download please try later')
return false
end
end
local function requiref(module)
require(module)
end
res = pcall(requiref,extended)
if not(res) then
local ans = fhMessageBox(
'This plugin requires '..module..' support, please click OK to download and install the module',
'MB_OKCANCEL','MB_ICONEXCLAMATION')
if ans ~= 'OK' then
return false
end
if installmodule(module) then
package.loaded[extended] = nil -- Reset Failed Load
require (extended)
else
return false
end
end
return true
end
function OpenFile(strFileName,strMode)
-- Open File and return Handle --
local fileHandle, strError = io.open(strFileName,strMode)
if not fileHandle then
error("\n Unable to open file in \""..strMode.."\" mode. \n "..strFileName.." \n "..tostring(strError).." \n")
end
return fileHandle
end -- function OpenFile
function FileExists(strFileName)
local fileHandle = io.open(strFileName,"r")
if fileHandle ~= nil then
io.close(fileHandle)
return true
else
return false
end
end
function LoadFromFile(strFileName)
-- Load string from file --
local fileHandle = OpenFile(strFileName,"rb")
local strString = fileHandle:read("*all")
assert(fileHandle:close())
return strString
end -- function LoadFromFile
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
-------------------------------------- SQL Lite Functions
function opendb(dbname)
-- Check for Settings Database and create if needed
local db = fhGetPluginDataFileName()
local dbenv = assert (luasql.sqlite3())
-- connect to data source, if the file does not exist it will be created
dbcon = assert (dbenv:connect(db))
checkTable(dbcon,'medialist',
[[CREATE TABLE medialist(recordid BIGINT,
md5 varchar(100), UNIQUE (recordid))
]])
return dbenv,dbcon
end
function checkTable(dbcon,table,createString)
local sql = string.format([[SELECT count(name) as count FROM sqlite_master WHERE type='table' AND name='%s']],table)
local cur = assert(dbcon:execute(sql))
local rowcount = cur:fetch (row, "a")
cur:close()
if tonumber(rowcount) == 0 then
-- Table not found create it
res,err = assert(dbcon:execute(createString))
end
end
function closedb(dbenv,dbcon)
dbcon:close()
dbenv:close()
end
function countrecords(dbcon)
local sql = [[SELECT count(recordid) as count FROM medialist]]
local cur,err = assert(dbcon:execute(sql))
local row = cur:fetch({},'a')
cur:close()
if row then
return tonumber(row['count'])
else
return false
end
end
function chkrecord(dbcon,recordid)
local sql = string.format([[SELECT * FROM medialist where recordid=%s]],recordid)
local cur,err = assert(dbcon:execute(sql))
local row = cur:fetch({},'a')
cur:close()
if row then
return row['md5']
else
return false
end
end
function updaterecord(dbcon,recordid,md5)
local sql = string.format("INSERT OR IGNORE INTO medialist (recordid,md5) VALUES (%s,'%s')",recordid,md5)
local res = assert(dbcon:execute(sql))
end
function clearlist(dbcon)
local sql = "DELETE FROM medialist"
local res = assert(dbcon:execute(sql))
end
-------------------------------------- Call Main Function
require 'lfs'
require 'luacom'
if not(loadrequire('md5')) then return end
if not(loadrequire('luasql','luasql.sqlite3')) then return end
mainfunction()