def testGetModifiedTime(self): tzname = "America/Argentina/Buenos_Aires" with self.settings(USE_TZ=False, TIME_ZONE=tzname), self.save_file(): modified_time = default_storage.get_modified_time("foo.txt") self.assertTrue(is_naive(modified_time)) # Check that the timestamps are roughly equals in the correct timezone self.assertLess(abs(modified_time - timezone.now()), timedelta(seconds=10)) # All other timestamps are slaved to modified time. self.assertEqual(default_storage.get_accessed_time("foo.txt"), modified_time) self.assertEqual(default_storage.get_created_time("foo.txt"), modified_time) with self.save_file(): modified_time = default_storage.get_modified_time("foo.txt") self.assertFalse(is_naive(modified_time)) # Check that the timestamps are roughly equals self.assertLess(abs(modified_time - timezone.now()), timedelta(seconds=10)) # All other timestamps are slaved to modified time. self.assertEqual(default_storage.get_accessed_time("foo.txt"), modified_time) self.assertEqual(default_storage.get_created_time("foo.txt"), modified_time)
def test_get_modified_time(self): tzname = "Asia/Shanghai" with self.settings(USE_TZ = False, TIME_ZONE = tzname), self.save_file(): modified_time = default_storage.get_modified_time("test.txt") logging.info("modified time: %s", modified_time) logging.info("is naive: %s", is_naive(modified_time)) self.assertTrue(is_naive(modified_time)) # Check that the timestamps are roughly equals in the correct timezone self.assertLess(abs(modified_time - timezone.now()), timedelta(seconds=10)) self.assertEqual(default_storage.get_accessed_time("test.txt"), modified_time) self.assertEqual(default_storage.get_created_time("test.txt"), modified_time) with self.settings(USE_TZ = True, TIME_ZONE = tzname), self.save_file(): modified_time = default_storage.get_modified_time("test.txt") logging.info("modified time: %s", modified_time) logging.info("is naive: %s", is_naive(modified_time)) self.assertFalse(is_naive(modified_time)) # Check that the timestamps are roughly equals in the correct timezone self.assertLess(abs(modified_time - timezone.now()), timedelta(seconds=10)) self.assertEqual(default_storage.get_accessed_time("test.txt"), modified_time) self.assertEqual(default_storage.get_created_time("test.txt"), modified_time) with self.save_file(): modified_time = default_storage.get_modified_time("test.txt") logging.info("modified time: %s", modified_time) logging.info("is naive: %s", is_naive(modified_time)) self.assertFalse(is_naive(modified_time)) # Check that the timestamps are roughly equals in the correct timezone self.assertLess(abs(modified_time - timezone.now()), timedelta(seconds=10)) self.assertEqual(default_storage.get_accessed_time("test.txt"), modified_time) self.assertEqual(default_storage.get_created_time("test.txt"), modified_time)
def check_availability(self): """ Perform check against Default Storage. """ try: name = default_storage.get_valid_name('Informer Storage') # Save data. content = ContentFile('File used by StorageInformer checking.') path = default_storage.save(name, content) # Check properties. default_storage.size(path) default_storage.url(path) default_storage.path(path) default_storage.get_accessed_time(path) default_storage.get_available_name(path) default_storage.get_created_time(path) default_storage.get_modified_time(path) default_storage.get_valid_name(path) # And remove file. default_storage.delete(path) storage = default_storage.__class__.__name__ except Exception as error: raise InformerException( f'An error occurred when trying to use your Storage: {error}') else: return True, f'Your {storage} is operational.'
def search(base_dir, matchpattern, dir_search=False, latest=False): """ :param base_dir: directory to search :param matchpattern: filename pattern to search for :param dir_search: flag to search for directories rather than files :param latest: flag to return only a single, most recently modified search result :return: If base directory doesn't exist: False If base directory exists, but is empty: Empty list If base directory exists with files and latest==False: list of matched files If base directory exists, latest == True, and files found: String containing filename If base directory exists, latest == True, and files not found: Empty string """ try: directories, files = default_storage.listdir(base_dir) except FileNotFoundError: return False if dir_search: search_list = directories else: search_list = files if search_list: regex = re.compile(matchpattern) file_filter = filter(regex.search, search_list) matchfiles = [f for f in file_filter] # Find most recent file if not latest: return matchfiles times = [(default_storage.get_modified_time(name=os.path.join(base_dir, i)), os.path.join(base_dir, i)) for i in matchfiles] if times: _, latestfile = max(times) else: latestfile = '' return latestfile return []
def update_index(): subdomain_index_file_re = re.compile(r'^(%s)(-(?P<language>uk))?\.xml$' % '|'.join( Region.objects.filter(subdomain=True).exclude(static_url=';').values_list('slug', flat=True) )) domain_file_re = re.compile(r'^mesto(-(?P<language>uk))?-(%s)(-\d+)?\.xml$' % '|'.join( [sitemap_class.section_name for sitemap_class in sitemap_section_classes] )) sitemaps = {'ru': [], 'uk': []} dirs, files = default_storage.listdir('sitemaps') for file_ in files: path = 'sitemaps/%s' % file_ # сильно устаревший sitemap (бывает при отключении у региона поддомена) if (datetime.datetime.now() - default_storage.get_modified_time(path)).days > 7: default_storage.delete(path) else: subdomain_index_file_match = subdomain_index_file_re.match(file_) if subdomain_index_file_match: language = subdomain_index_file_match.group('language') or 'ru' sitemaps[language].append(path) domain_file_match = domain_file_re.match(file_) if domain_file_match: language = domain_file_match.group('language') or 'ru' sitemaps[language].append(path) for language, pathes in sitemaps.items(): xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': [default_storage.url(path) for path in pathes]}) index_path = 'sitemaps/mesto%s.xml' % {'ru': '', 'uk': '-uk'}[language] overwrite(index_path, ContentFile(xml)) print 'updated', index_path
def get_fileinfo(f): filename = os.path.split(f)[1] filetype = get_filetype(filename) return { 'name': filename, 'type': filetype, 'preview': filetype in ('image', 'audio', 'video'), 'size': default_storage.size(f), 'date': default_storage.get_modified_time(f), 'url': default_storage.url(f), }
def get_file_modified_time_utc(file_path): """ Gets the UTC timezone-aware modified time of a file at the given file path """ file_timezone = ( # time.tzname returns a 2 element tuple: # (local non-DST timezone, e.g.: 'EST', local DST timezone, e.g.: 'EDT') pytz.timezone(time.tzname[0]) if settings.DEFAULT_FILE_STORAGE == 'django.core.files.storage.FileSystemStorage' else pytz.utc) return file_timezone.localize( default_storage.get_modified_time(file_path)).astimezone(pytz.utc)
def file_response(): if not default_storage.exists(filename): raise FileNotFoundError if time.time() - default_storage.get_modified_time( filename).timestamp() > settings.ICS_EXPIRATION: raise ExpiredFileError response = FileResponse(default_storage.open(filename), as_attachment=True, content_type='text/calendar', filename=f'{lc.username}.ics') response.setdefault('Content-Length', default_storage.size(filename)) return response
def test_create_thumbnails_command(self): call_command("create_image_thumbnails") created_times = {} for attachment in Attachment.objects.filter(instance=self.instance): filename = attachment.media_file.name.replace('.jpg', '') for size in settings.THUMB_CONF.keys(): thumbnail = '%s-%s.jpg' % (filename, size) self.assertTrue(default_storage.exists(thumbnail)) created_times[size] = default_storage.get_modified_time( thumbnail) # replace or regenerate thumbnails if they exist call_command("create_image_thumbnails", force=True) for attachment in Attachment.objects.filter(instance=self.instance): filename = attachment.media_file.name.replace('.jpg', '') for size in settings.THUMB_CONF.keys(): thumbnail = f'{filename}-{size}.jpg' self.assertTrue(default_storage.exists(thumbnail)) self.assertTrue( default_storage.get_modified_time(thumbnail) > created_times[size]) default_storage.delete(thumbnail)
def thumbnail(file, size='200x200'): # defining the size x, y = [int(x) for x in size.split('x')] # defining the filename and the miniature filename filehead, filetail = os.path.split(file.name) basename, format = os.path.splitext(filetail) miniature = basename + '_' + size + format filename = file.name miniature_filename = os.path.join(filehead, miniature) filehead, filetail = os.path.split(file.url) miniature_url = filehead + '/' + miniature thumbnail_exist = False if default_storage.exists(miniature_filename): mt_filename = default_storage.get_modified_time(filename) mt_miniature_filename = default_storage.get_modified_time( miniature_filename) if mt_filename > mt_miniature_filename: # remove the miniature default_storage.delete(miniature_filename) else: thumbnail_exist = True # if the image wasn't already resized, resize it if not thumbnail_exist: if not default_storage.exists(filename): return u'' image = Image.open(default_storage.open(filename)) image.thumbnail([x, y], Image.ANTIALIAS) f = default_storage.open(miniature_filename, 'w') image.save(f, image.format, quality=90, optimize=1) f.close() return miniature_url
def thumbnail(file, size='200x200'): # defining the size x, y = [int(x) for x in size.split('x')] # defining the filename and the miniature filename filehead, filetail = os.path.split(file.name) basename, format = os.path.splitext(filetail) miniature = basename + '_' + size + format filename = file.name miniature_filename = os.path.join(filehead, miniature) filehead, filetail = os.path.split(file.url) miniature_url = filehead + '/' + miniature thumbnail_exist = False if default_storage.exists(miniature_filename): mt_filename = default_storage.get_modified_time(filename) mt_miniature_filename = default_storage.get_modified_time( miniature_filename) if mt_filename > mt_miniature_filename: # remove the miniature default_storage.delete(miniature_filename) else: thumbnail_exist = True # if the image wasn't already resized, resize it if not thumbnail_exist: if not default_storage.exists(filename): return u'' image = Image.open(default_storage.open(filename)) image.thumbnail([x, y], Image.ANTIALIAS) f = default_storage.open(miniature_filename, 'w') image.save(f, image.format, quality=90, optimize=1) f.close() return miniature_url
def list_files_info(): """ Function returns a list of dictionaries for each file in 'saved_data' directory. Each file contains file name, created time and modified time. """ _, filenames = default_storage.listdir('saved_data') files = [{ 'name': filename, 'created': default_storage.get_created_time(f'saved_data/{filename}'), 'modified': default_storage.get_modified_time(f'saved_data/{filename}') } for filename in filenames if re.search('^[a-z0-9]', filename)] return files
def handle(self, *args, **options): try: dirs, files = default_storage.listdir(settings.AJAX_UPLOAD_DIR) except OSError: # no directory exception for FileSystemStorage files = [] deleted = 0 utc_now = datetime.datetime.utcnow() now = datetime.datetime.now() for name in files: path = '%s/%s' % (settings.AJAX_UPLOAD_DIR, name) file_mtime = default_storage.get_modified_time(path) if (utc_now - file_mtime).seconds > 3600 * 6: default_storage.delete(path) print 'remove %s' % path deleted += 1 print '%s: %d out-of-date files removed' % (now, deleted)
def directory_cleanup(dir_path, ndays): """ Delete the files that are older than 'ndays' in the directory 'dir_path' The 'dir_path' should be a relative path. We cannot use os.walk. """ if not default_storage.exists(dir_path): return foldernames, filenames = default_storage.listdir(dir_path) for filename in filenames: if not filename: continue file_path = os.path.join(dir_path, filename) modified_dt = default_storage.get_modified_time(file_path) if modified_dt + timedelta(days=ndays) < datetime.now(): # the file is older than ndays, delete it default_storage.delete(file_path) for foldername in foldernames: folder_path = os.path.join(dir_path, foldername) directory_cleanup(folder_path, ndays)
def test_create_thumbnails_command(self): call_command("create_image_thumbnails") for attachment in Attachment.objects.filter(instance=self.instance): filename = attachment.media_file.name.replace('.jpg', '') for size in ['small', 'medium', 'large']: thumbnail = '%s-%s.jpg' % (filename, size) self.assertTrue(default_storage.exists(thumbnail)) check_datetime = timezone.now() # replace or regenerate thumbnails if they exist call_command("create_image_thumbnails", force=True) for attachment in Attachment.objects.filter(instance=self.instance): filename = attachment.media_file.name.replace('.jpg', '') for size in ['small', 'medium', 'large']: thumbnail = '%s-%s.jpg' % (filename, size) self.assertTrue(default_storage.exists(thumbnail)) self.assertTrue( default_storage.get_modified_time(thumbnail) > check_datetime) default_storage.delete(thumbnail)
def directory_cleanup(dir_path, ndays): """ Delete the files that are older than 'ndays' in the directory 'dir_path' The 'dir_path' should be a relative path. We cannot use os.walk. """ if not default_storage.exists(dir_path): return foldernames, filenames = default_storage.listdir(dir_path) for filename in filenames: if not filename: continue file_path = os.path.join(dir_path, filename) modified_dt = default_storage.get_modified_time(file_path) if modified_dt + timedelta(days=ndays) < datetime.now(): # the file is older than ndays, delete it default_storage.delete(file_path) for foldername in foldernames: folder_path = os.path.join(dir_path, foldername) directory_cleanup(folder_path, ndays)
def test_create_thumbnails_command(self): call_command("create_image_thumbnails") for attachment in Attachment.objects.filter(instance=self.instance): filename = attachment.media_file.name.replace('.jpg', '') for size in ['small', 'medium', 'large']: thumbnail = '%s-%s.jpg' % (filename, size) self.assertTrue( default_storage.exists(thumbnail)) check_datetime = timezone.now() # replace or regenerate thumbnails if they exist call_command("create_image_thumbnails", force=True) for attachment in Attachment.objects.filter(instance=self.instance): filename = attachment.media_file.name.replace('.jpg', '') for size in ['small', 'medium', 'large']: thumbnail = '%s-%s.jpg' % (filename, size) self.assertTrue( default_storage.exists(thumbnail)) self.assertTrue( default_storage.get_modified_time(thumbnail) > check_datetime ) default_storage.delete(thumbnail)
def get_file_modified_time_utc(file_path): return default_storage.get_modified_time(file_path)
def get_modified_time(self, name): return default_storage.get_modified_time(name)
def date(self): if self.exists: return time.mktime( default_storage.get_modified_time(self.path).timetuple()) return None
def get_last_modified(self): return default_storage.get_modified_time( self.kwargs['path']).timestamp()
def date(self): if self.exists: return time.mktime( default_storage.get_modified_time(self.path).timetuple()) return None