Files
FixIt/layouts/partials/function/get-remote-image.html
T
2025-01-19 04:13:10 +08:00

23 lines
976 B
HTML

{{- /* cache remote image locally */ -}}
{{- $resource := 0 -}}
{{- $suffixList := slice ".jpeg" ".jpg" ".png" ".gif" ".bmp" ".tif" ".tiff" ".webp" ".avif" ".svg" -}}
{{- $suffixValid := (dict "Path" .Path "Suffixes" $suffixList | partial "function/suffix-validation.html") -}}
{{- /* maybe could add domain validation */ -}}
{{- $pass := $suffixValid | or .Pass -}}
{{- if $pass -}}
{{- with try ($remoteResource := resources.GetRemote .Path) -}}
{{- with .Err -}}
{{- erroridf "error-get-remote-image" "%s" . -}}
{{- else with .Value -}}
{{- if eq $remoteResource.ResourceType "image" -}}
{{- /* placed remote image in public/images/remote/ */ -}}
{{- $resource = $remoteResource | resources.Copy (add "/images/remote/" $remoteResource.Name) -}}
{{- end -}}
{{- else -}}
{{- erroridf "error-get-remote-image" "Unable to get remote image %q" $.Path -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- return $resource -}}