Skip to content

Commit f99674b

Browse files
committed
fix file renaming after task completion
1 parent 43fc55c commit f99674b

File tree

1 file changed

+25
-64
lines changed

1 file changed

+25
-64
lines changed

xeHentai/task.py

Lines changed: 25 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -258,30 +258,14 @@ def save_file(self, imgurl, redirect_url, binary_iter):
258258

259259
self._f_lock.acquire()
260260
try:
261-
try:
262-
shutil.move(fn_tmp, fn)
263-
except WindowsError as ex:
264-
# file is used by another process
265-
# do a copy and delete, WindowsError[32]
266-
if ex.errno == 13:
267-
shutil.copy(fn_tmp, fn)
268-
try:
269-
os.unlink(fn_tmp)
270-
except:
271-
pass
272-
else:
273-
raise ex
261+
# Move the temp file to the final destination
262+
shutil.move(fn_tmp, fn)
274263
self.set_fid_finished(fid)
275-
if fid in self.duplicate_map:
276-
for fid_rep in self.duplicate_map[fid]:
277-
# if a file download is interrupted, it will appear in self.filehash_map as well
278-
if fid_rep == fid:
279-
continue
280-
fn_rep = os.path.join(fpath, self.get_fidpad(fid_rep))
281-
shutil.copyfile(fn, fn_rep)
282-
self.set_fid_finished(fid_rep)
283-
self.logger.debug("#%s is copied from #%s in save_file" % (fid_rep, fid))
284-
del self.duplicate_map[fid]
264+
265+
# Store the actual downloaded file name and its extension
266+
actual_ext = os.path.splitext(fname)[1] or '.jpg' # Default to .jpg if no extension
267+
self.renamed_map[fid] = fname # Store the complete filename for renaming later
268+
285269
except Exception as ex:
286270
self._f_lock.release()
287271
raise ex
@@ -331,60 +315,37 @@ def rename_fname(self):
331315
tmppath = os.path.join(fpath, RENAME_TMPDIR)
332316
cnt = 0
333317
error_list = []
334-
# we need to track renamed fid's to decide
335-
# whether to rename into a temp filename or add (1)
336-
# only need it when rename_ori = True
337-
done_list = set()
338318
for fid in list(self.renamed_map.keys()):
339-
fname = self.renamed_map[fid]
340-
original_ext = os.path.splitext(fname)[1]
341-
if original_ext== "":
342-
original_ext = os.path.splitext(fname)[0]
343-
# if we don't need to rename to original name and file type matches
344-
if not self.config['rename_ori'] and original_ext.lower() == '.jpg':
345-
continue
346-
fname_ori = os.path.join(fpath, self.get_fidpad(fid)) # id
319+
# Check if rename_ori is enabled
347320
if self.config['rename_ori']:
348-
if os.path.exists(os.path.join(tmppath, self.get_fidpad(fid))):
349-
# if we previously put it into a temporary folder, we need to change fname_ori
350-
fname_ori = os.path.join(tmppath, self.get_fidpad(fid))
351-
fname_to = os.path.join(fpath, util.legalpath(fname))
321+
# Use the stored full filename
322+
fname_new = os.path.join(fpath, util.legalpath(self.renamed_map[fid]))
323+
fname_ori = os.path.join(fpath, self.get_fidpad(fid)) # Original filename with ID
352324
else:
353325
# Q: Why we don't just use id.ext when saving files instead of using
354326
# id.jpg?
355327
# A: If former task doesn't download all files, a new task with same gallery
356328
# will have zero knowledge about file type before scanning all per page,
357329
# thus can't determine if this id is downloaded, because file type is not
358330
# necessarily .jpg
359-
fname_to = os.path.join(fpath, self.get_fidpad(fid, original_ext[1:]))
360-
while fname_ori != fname_to:
331+
#
332+
# Use the previously stored extension
333+
original_ext = os.path.splitext(self.renamed_map[fid])[1] # Get the extension from the stored filename
334+
fname_ori = os.path.join(fpath, self.get_fidpad(fid)) # ID-based filename
335+
fname_new = os.path.join(fpath, self.get_fidpad(fid, original_ext[1:])) # Use the stored extension
336+
337+
while fname_ori != fname_new:
361338
if os.path.exists(fname_ori):
362-
while os.path.exists(fname_to):
363-
_base, _ext = os.path.splitext(fname_to)
364-
_ = re.findall("\((\d+)\)$", _base)
365-
if self.config['rename_ori'] and fname_to not in done_list:
366-
# if our auto numbering conflicts with original naming
367-
# we move it into a temporary folder
368-
# It's safe since this file is same with one of our auto numbering filename,
369-
# it could never be conflicted with other files in tmppath
370-
if not os.path.exists(tmppath):
371-
os.mkdir(tmppath)
372-
os.rename(fname_to, os.path.join(tmppath, os.path.split(fname_to)[1]))
373-
break
374-
if _ :# if ...(1) exists, use ...(2)
375-
print(safestr(_base))
376-
_base = re.sub("\((\d+)\)$", _base, lambda x:"(%d)" % (int(x.group(1)) + 1))
377-
else:
378-
_base = "%s(1)" % _base
379-
fname_to = "".join((_base, _ext))
339+
while os.path.exists(fname_new):
340+
_base, _ext = os.path.splitext(fname_new)
341+
_base = "%s(1)" % _base
342+
fname_new = "".join((_base, original_ext))
380343
try:
381-
os.rename(fname_ori, fname_to)
382-
self.renamed_map[str(fid)] = os.path.split(fname_to)[1]
344+
os.rename(fname_ori, fname_new)
345+
self.renamed_map[str(fid)] = os.path.split(fname_new)[1]
383346
except Exception as ex:
384-
error_list.append((os.path.split(fname_ori)[1], os.path.split(fname_to)[1], str(ex)))
347+
error_list.append((os.path.split(fname_ori)[1], os.path.split(fname_new)[1], str(ex)))
385348
break
386-
if self.config['rename_ori']:
387-
done_list.add(fname_to)
388349
break
389350
cnt += 1
390351
if cnt == self.meta['total']:

0 commit comments

Comments
 (0)