models.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # import mimetypes
  2. # import uuid
  3. # from datetime import timedelta
  4. # from pathlib import Path
  5. # from django.db import models
  6. # from django.conf import settings
  7. # from django.utils import timezone
  8. # from archivebox import DATA_DIR
  9. # from archivebox.misc.hashing import get_dir_info, hash_file
  10. # from base_models.abid import DEFAULT_ABID_URI_SALT
  11. # from base_models.models import ABIDModel, ABIDField, get_or_create_system_user_pk
  12. # class File(ABIDModel):
  13. # abid_prefix = 'fil_'
  14. # abid_ts_src = 'self.created_at'
  15. # abid_uri_src = 'self.path'
  16. # abid_subtype_src = 'self.mime_type'
  17. # abid_rand_src = 'self.id'
  18. # abid_salt: str = DEFAULT_ABID_URI_SALT # combined with self.uri to anonymize hashes on a per-install basis (default is shared globally with all users, means everyone will hash ABC to -> 123 the same around the world, makes it easy to share ABIDs across installs and see if they are for the same URI. Change this if you dont want your hashes to be guessable / in the same hash space as all other users)
  19. # abid_drift_allowed: bool = False
  20. # id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, null=False)
  21. # abid = ABIDField(prefix=abid_prefix)
  22. # created_at = models.DateTimeField(default=timezone.now, null=False)
  23. # modified_at = models.DateTimeField(default=timezone.now, null=False)
  24. # created_by = models.ForeignKey(settings.USER_MODEL, on_delete=models.CASCADE, default=get_or_create_system_user_pk)
  25. # class StatusChoices(models.TextChoices):
  26. # UNLOCKED = 'unlocked'
  27. # LOCKED = 'locked'
  28. # status = models.CharField(max_length=16, choices=StatusChoices.choices, default=StatusChoices.UNLOCKED, null=False)
  29. # retry_at = models.DateTimeField(default=None, null=True)
  30. # version = models.CharField(max_length=16, default='unknown', null=False)
  31. # file = models.FileField(null=False)
  32. # basename = models.CharField(max_length=255, default=None, null=False) # e.g. 'index'
  33. # extension = models.CharField(max_length=63, default='', null=False) # e.g. 'html'
  34. # mime_type = models.CharField(max_length=63, default=None, null=False, db_index=True) # e.g. 'inode/directory' or 'text/html'
  35. # num_subpaths = models.IntegerField(default=None, null=False) # e.g. 3
  36. # num_bytes = models.IntegerField(default=None, null=False) # e.g. 123456
  37. # sha256 = models.CharField(max_length=64, default=None, null=False, db_index=True) # e.g. '5994471abb01112afcc1815994471abb01112afcc1815994471abb01112afcc181'
  38. # # blake3 = models.CharField(max_length=64, default=None, null=False, db_index=True) # e.g. '5994471abb01112afcc1815994471abb01112afcc1815994471abb01112afcc181'
  39. # DIR = 'inode/directory'
  40. # @classmethod
  41. # def release_expired_locks(cls):
  42. # cls.objects.filter(status='locked', retry_at__lt=timezone.now()).update(status='unlocked', retry_at=None)
  43. # @property
  44. # def parent(self) -> 'File':
  45. # return File.objects.get(path=str(self.PATH.parent)) or File(path=str(self.PATH.parent))
  46. # @property
  47. # def relpath(self) -> Path:
  48. # return Path(self.file.name)
  49. # @property
  50. # def abspath(self) -> Path:
  51. # return DATA_DIR / self.file.name
  52. # def save(self, *args, **kwargs):
  53. # assert self.abspath.exists()
  54. # if self.abspath.is_dir():
  55. # self.basename = self.relpath.name
  56. # self.extension = ''
  57. # self.mime_type = self.DIR
  58. # dir_info = get_dir_info(self.abspath)
  59. # self.num_subpaths = dir_info['.']['num_subpaths']
  60. # self.num_bytes = dir_info['.']['num_bytes']
  61. # self.hash_sha256 = dir_info['.']['hash_sha256']
  62. # # TODO: hash_blake3 = dir_info['.']['hash_blake3']
  63. # else:
  64. # self.basename = self.relpath.name
  65. # self.extension = self.relpath.suffix
  66. # self.mime_type = mimetypes.guess_type(self.abspath)[0]
  67. # self.num_bytes = self.abspath.stat().st_size
  68. # self.hash_sha256, self.hash_blake3 = hash_file(self.abspath)
  69. # super().save(*args, **kwargs)
  70. # def acquire_lock(self, timeout_seconds: int = 60):
  71. # self.status = 'locked'
  72. # self.retry_at = timezone.now() + timedelta(seconds=timeout_seconds)
  73. # self.save()
  74. # def release_lock(self):
  75. # self.status = 'unlocked'
  76. # self.retry_at = None
  77. # self.save()
  78. # def move_to(self, new_path: Path):
  79. # if str(new_path).startswith(str(DATA_DIR)):
  80. # new_relpath = new_path.relative_to(DATA_DIR)
  81. # new_abspath = new_path
  82. # else:
  83. # new_relpath = new_path
  84. # new_abspath = DATA_DIR / new_path
  85. # new_abspath.parent.mkdir(parents=True, exist_ok=True)
  86. # self.abspath.rename(new_abspath)
  87. # self.file.name = new_relpath
  88. # self.save()