Implement basic node side image caching

This commit is contained in:
2023-06-12 23:42:25 +02:00
parent 3f2473d7e0
commit 1676bbb886
5 changed files with 25 additions and 3 deletions

View File

@@ -3,17 +3,33 @@ extends HTTPRequest
func GetRemoteImage(url):
var image = Image.new()
print("Fetching remote image %s" % url)
var urlHash = str(url.hash())
var outFile = "{dir}/{hash}-{fileName}.png".format({
'dir': $"/root/Main".cache_dir,
'hash': urlHash,
'fileName': url.get_file().rsplit(".", true, 1)[0]
})
if FileAccess.file_exists(outFile):
image.load(outFile)
print("Loaded image from cache. " + outFile)
return image
timeout = 1000
request(url)
var res = await request_completed
var magicBytes = res[3].slice(0,8)
print(magicBytes)
var fileType = url.get_extension()
var error = null
if magicBytes == PackedByteArray([137, 80, 78, 71, 13, 10, 26, 10]):
if fileType == "png" or magicBytes == PackedByteArray([137, 80, 78, 71, 13, 10, 26, 10]):
print("Detected PNG File")
error = image.load_png_from_buffer(res[3])
if magicBytes == PackedByteArray([255, 216, 255, 224, 0, 16, 74, 70]):
if ["jpg", "jpeg"].has(fileType) or magicBytes == PackedByteArray([255, 216, 255, 224, 0, 16, 74, 70]):
print("Detected JPG File")
error = image.load_jpg_from_buffer(res[3])
if error != OK:
print("Error fetching image ", str(error))
image.save_png(outFile)
print("Saved new image to cache. " + outFile)
return image