q6 commited on
Commit
8b673ee
·
1 Parent(s): d218528

Preserve uploaded asset numbers

Browse files
Files changed (1) hide show
  1. app.py +29 -14
app.py CHANGED
@@ -457,8 +457,8 @@ def index_bucket_models():
457
  return added
458
 
459
 
460
- def next_bucket_number(kind, anima):
461
- numbers = []
462
  for item in list_bucket_tree(
463
  BUCKET_ID,
464
  prefix=f"{kind}/",
@@ -469,8 +469,8 @@ def next_bucket_number(kind, anima):
469
  continue
470
  match = MODEL_NUMBER.match(Path(item.path).name)
471
  if match and is_anima_asset(match.group(2)) == anima:
472
- numbers.append(int(match.group(1)))
473
- return max(numbers, default=0) + 1
474
 
475
 
476
  def upload_bucket_assets(files, kind, anima, password):
@@ -484,24 +484,39 @@ def upload_bucket_assets(files, kind, anima, password):
484
  assets = []
485
  for file in files:
486
  source = Path(file)
487
- name = source.name
488
- match = MODEL_NUMBER.match(name)
489
- name = match.group(2) if match else name
490
  if source.suffix.casefold() not in MODEL_SUFFIXES:
491
  raise gr.Error(f"Unsupported model file: {name}")
492
  if anima and not is_anima_asset(name):
493
  name = f"{ANIMA_PREFIX}_{name}"
494
- assets.append((source, name))
495
 
496
  with model_upload_lock:
497
- number = next_bucket_number(kind, anima)
498
- paths = [
499
- f"{kind}/{number + index}_{name}"
500
- for index, (_, name) in enumerate(assets)
501
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
502
  batch_bucket_files(
503
  BUCKET_ID,
504
- add=[(source, path) for (source, _), path in zip(assets, paths)],
505
  token=IMAGE_TOKEN,
506
  )
507
  index_bucket_models()
 
457
  return added
458
 
459
 
460
+ def bucket_numbers(kind, anima):
461
+ numbers = set()
462
  for item in list_bucket_tree(
463
  BUCKET_ID,
464
  prefix=f"{kind}/",
 
469
  continue
470
  match = MODEL_NUMBER.match(Path(item.path).name)
471
  if match and is_anima_asset(match.group(2)) == anima:
472
+ numbers.add(int(match.group(1)))
473
+ return numbers
474
 
475
 
476
  def upload_bucket_assets(files, kind, anima, password):
 
484
  assets = []
485
  for file in files:
486
  source = Path(file)
487
+ match = MODEL_NUMBER.match(source.name)
488
+ number, name = (int(match.group(1)), match.group(2)) \
489
+ if match else (None, source.name)
490
  if source.suffix.casefold() not in MODEL_SUFFIXES:
491
  raise gr.Error(f"Unsupported model file: {name}")
492
  if anima and not is_anima_asset(name):
493
  name = f"{ANIMA_PREFIX}_{name}"
494
+ assets.append((source, number, name))
495
 
496
  with model_upload_lock:
497
+ used = bucket_numbers(kind, anima)
498
+ reserved = {
499
+ number for _, number, _ in assets
500
+ if number is not None and number not in used
501
+ }
502
+ next_number = max(used, default=0) + 1
503
+ additions = []
504
+ paths = []
505
+ for source, number, name in assets:
506
+ if number is not None and number in reserved:
507
+ reserved.remove(number)
508
+ else:
509
+ while next_number in used or next_number in reserved:
510
+ next_number += 1
511
+ number = next_number
512
+ next_number += 1
513
+ used.add(number)
514
+ path = f"{kind}/{number}_{name}"
515
+ additions.append((source, path))
516
+ paths.append(path)
517
  batch_bucket_files(
518
  BUCKET_ID,
519
+ add=additions,
520
  token=IMAGE_TOKEN,
521
  )
522
  index_bucket_models()