2022-09-24 06:40:26 +00:00
|
|
|
import os
|
|
|
|
import zlib
|
|
|
|
from collections import defaultdict
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-09-27 06:02:02 +00:00
|
|
|
import gevent
|
|
|
|
import gevent_inotifyx as inotify
|
2022-09-24 06:40:26 +00:00
|
|
|
|
2023-03-12 13:03:58 +00:00
|
|
|
from files.helpers.config.const import *
|
2022-11-29 22:01:25 +00:00
|
|
|
|
2022-09-24 06:40:26 +00:00
|
|
|
ASSET_DIR = 'files/assets'
|
2022-11-22 00:02:39 +00:00
|
|
|
ASSET_SUBDIRS = ['/css', '/js', '/js/vendor']
|
2022-09-24 06:40:26 +00:00
|
|
|
ASSET_URL = '/assets/'
|
|
|
|
ASSET_CACHE = defaultdict(lambda: None)
|
|
|
|
|
2023-03-08 06:51:40 +00:00
|
|
|
if IS_FISTMAS():
|
2023-01-28 11:33:51 +00:00
|
|
|
ASSET_SUBDIRS.extend(['/fistmas/css', '/fistmas/css/themes', '/fistmas/js', '/fistmas/js/vendor'])
|
2022-11-29 22:01:25 +00:00
|
|
|
|
2022-09-24 06:40:26 +00:00
|
|
|
def assetcache_build(asset_dir, subdirs):
|
|
|
|
for subdir in subdirs:
|
|
|
|
for root, dirs, files in os.walk(asset_dir + subdir):
|
|
|
|
for fname in files:
|
|
|
|
fpath = root + '/' + fname
|
|
|
|
relpath = fpath[len(asset_dir) + 1:].replace('\\', '/')
|
|
|
|
with open(fpath, 'rb') as f:
|
|
|
|
fhash = zlib.crc32(f.read())
|
|
|
|
ASSET_CACHE[relpath] = '%x' % fhash
|
|
|
|
|
|
|
|
def assetcache_hash(asset_path):
|
|
|
|
return ASSET_CACHE[asset_path]
|
|
|
|
|
|
|
|
def assetcache_path(asset_path):
|
|
|
|
cachehash = assetcache_hash(asset_path)
|
|
|
|
|
|
|
|
url = ASSET_URL + asset_path
|
|
|
|
if cachehash:
|
|
|
|
url += '?v=' + cachehash
|
|
|
|
|
|
|
|
return url
|
|
|
|
|
2022-09-27 06:02:02 +00:00
|
|
|
def assetcache_watch_directories(asset_dir, subdirs):
|
|
|
|
fd = inotify.init()
|
|
|
|
try:
|
|
|
|
for sd in subdirs:
|
|
|
|
inotify.add_watch(fd, asset_dir + sd, inotify.IN_CLOSE_WRITE)
|
|
|
|
while True:
|
|
|
|
for event in inotify.get_events(fd, 0):
|
|
|
|
print("Rebuilding assetcache: " + event.name, flush=True)
|
|
|
|
assetcache_build(asset_dir, subdirs)
|
|
|
|
break
|
|
|
|
gevent.sleep(0.5)
|
|
|
|
finally:
|
|
|
|
os.close(fd)
|
|
|
|
|
2022-09-24 06:40:26 +00:00
|
|
|
assetcache_build(ASSET_DIR, ASSET_SUBDIRS)
|
2022-09-27 06:02:02 +00:00
|
|
|
gevent.spawn(assetcache_watch_directories, ASSET_DIR, ASSET_SUBDIRS)
|