Implement basic node side image caching
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,6 +3,7 @@
|
|||||||
.godot/
|
.godot/
|
||||||
/[Bb]uild/
|
/[Bb]uild/
|
||||||
/[Oo]utput/
|
/[Oo]utput/
|
||||||
|
/[Cc]ache/
|
||||||
# Godot-specific ignores
|
# Godot-specific ignores
|
||||||
.import/
|
.import/
|
||||||
export.cfg
|
export.cfg
|
||||||
|
|||||||
4
Main.gd
4
Main.gd
@@ -5,6 +5,7 @@ var config = ConfigFile.new()
|
|||||||
var websocket_url
|
var websocket_url
|
||||||
var public_path
|
var public_path
|
||||||
var output_dir
|
var output_dir
|
||||||
|
var cache_dir
|
||||||
var auth_key
|
var auth_key
|
||||||
var rendering = false
|
var rendering = false
|
||||||
signal finished_rendering
|
signal finished_rendering
|
||||||
@@ -15,11 +16,14 @@ func _ready():
|
|||||||
websocket_url = config.get_value("core", "websocket_url")
|
websocket_url = config.get_value("core", "websocket_url")
|
||||||
public_path = config.get_value("core", "public_path")
|
public_path = config.get_value("core", "public_path")
|
||||||
output_dir = config.get_value("core", "output_dir")
|
output_dir = config.get_value("core", "output_dir")
|
||||||
|
cache_dir = config.get_value("core", "cache_dir")
|
||||||
auth_key = config.get_value("core", "auth_key")
|
auth_key = config.get_value("core", "auth_key")
|
||||||
|
|
||||||
print("Starting render node")
|
print("Starting render node")
|
||||||
|
|
||||||
var directory = DirAccess.open(".")
|
var directory = DirAccess.open(".")
|
||||||
directory.make_dir(output_dir)
|
directory.make_dir(output_dir)
|
||||||
|
directory.make_dir(cache_dir)
|
||||||
$JobServer.InitWebsocket()
|
$JobServer.InitWebsocket()
|
||||||
|
|
||||||
func FinishedRendering():
|
func FinishedRendering():
|
||||||
|
|||||||
@@ -3,17 +3,33 @@ extends HTTPRequest
|
|||||||
func GetRemoteImage(url):
|
func GetRemoteImage(url):
|
||||||
var image = Image.new()
|
var image = Image.new()
|
||||||
print("Fetching remote image %s" % url)
|
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)
|
request(url)
|
||||||
var res = await request_completed
|
var res = await request_completed
|
||||||
var magicBytes = res[3].slice(0,8)
|
var magicBytes = res[3].slice(0,8)
|
||||||
print(magicBytes)
|
print(magicBytes)
|
||||||
|
var fileType = url.get_extension()
|
||||||
var error = null
|
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")
|
print("Detected PNG File")
|
||||||
error = image.load_png_from_buffer(res[3])
|
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")
|
print("Detected JPG File")
|
||||||
error = image.load_jpg_from_buffer(res[3])
|
error = image.load_jpg_from_buffer(res[3])
|
||||||
if error != OK:
|
if error != OK:
|
||||||
print("Error fetching image ", str(error))
|
print("Error fetching image ", str(error))
|
||||||
|
image.save_png(outFile)
|
||||||
|
print("Saved new image to cache. " + outFile)
|
||||||
return image
|
return image
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func RenderJob(job):
|
|||||||
|
|
||||||
# Get rendered image
|
# Get rendered image
|
||||||
var img = get_viewport().get_texture().get_image()
|
var img = get_viewport().get_texture().get_image()
|
||||||
var outFile = "%s_%s.png" % [job["type"],job["jobId"]]
|
var outFile = "%s_%s.png" % [job["type"],str(job["elements"]).hash()]
|
||||||
img.save_png("%s/%s" % [$"/root/Main".output_dir, outFile])
|
img.save_png("%s/%s" % [$"/root/Main".output_dir, outFile])
|
||||||
|
|
||||||
return outFile
|
return outFile
|
||||||
|
|||||||
@@ -3,3 +3,4 @@ auth_key=ay1234
|
|||||||
websocket_url="ws://localhost:6980"
|
websocket_url="ws://localhost:6980"
|
||||||
public_path="http://localhost:8899/"
|
public_path="http://localhost:8899/"
|
||||||
output_dir="./output"
|
output_dir="./output"
|
||||||
|
cache_dir="./cache"
|
||||||
|
|||||||
Reference in New Issue
Block a user