• Publicity: Public Only All

image-procs.tcl

Shared, disposable resized-image cache for attachment integrations.

Location:
packages/attachments/tcl/image-procs.tcl

Procedures in this file

Detailed information

attachments::image::variant (public)

 attachments::image::variant path width [ identity ]

Return a cached, smaller bitmap, or the original when conversion is unavailable. Callers must authorize access to the original BEFORE calling this helper. An empty width requests the original. Identity should include the CR revision ID for committed images; drafts are distinguished by their private path.

Parameters:
path (required)
width (required)
identity (optional)

Partial Call Graph (max 5 caller/called nodes):
packages/forums/www/markdown-image.tcl packages/forums/ www/markdown-image.tcl attachments::image::variant attachments::image::variant packages/forums/www/markdown-image.tcl->attachments::image::variant packages/news/www/markdown-image.tcl packages/news/ www/markdown-image.tcl packages/news/www/markdown-image.tcl->attachments::image::variant ad_log ad_log (public) attachments::image::variant->ad_log attachments::image::directory attachments::image::directory (private) attachments::image::variant->attachments::image::directory attachments::image::widths attachments::image::widths (public) attachments::image::variant->attachments::image::widths image::convert_binary image::convert_binary (public) attachments::image::variant->image::convert_binary

Testcases:
No testcase defined.

attachments::image::widths (public)

 attachments::image::widths

Supported responsive image widths in pixels. Keep in sync with the Markdown editor.

Partial Call Graph (max 5 caller/called nodes):
attachments::image::variant attachments::image::variant (public) attachments::image::widths attachments::image::widths attachments::image::variant->attachments::image::widths packages/forums/www/markdown-image.tcl packages/forums/ www/markdown-image.tcl packages/forums/www/markdown-image.tcl->attachments::image::widths packages/news/www/markdown-image.tcl packages/news/ www/markdown-image.tcl packages/news/www/markdown-image.tcl->attachments::image::widths

Testcases:
No testcase defined.
[ hide source ] | [ make this the default ]

Content File Source

ad_library {
    Shared, disposable resized-image cache for attachment integrations.
}

namespace eval ::attachments::image {}

ad_proc -public ::attachments::image::widths {} {
    Supported responsive image widths in pixels. Keep in sync with the Markdown editor.
} {
    return {320 640 960 1280 1920}
}

ad_proc -private ::attachments::image::directory {} {
    Return the private cache directory. Generated files are never public resources.
} {
    return [file join [ad_tmpdir] attachments-image-cache]
}

ad_proc -public ::attachments::image::variant {path width {identity ""}} {
    Return a cached, smaller bitmap, or the original when conversion is unavailable.
    Callers must authorize access to the original BEFORE calling this helper.
    An empty width requests the original. Identity should include the CR revision
    ID for committed images; drafts are distinguished by their private path.
} {
    if {$width eq ""} { return $path }
    if {$width ni [widths]} { error "Unsupported attachment image width" }
    if {![file isfile $path]
        || [catch {ns_imgtype $path} type] || $type ni {png jpeg gif}
        || [catch {ns_imgsize $path} dimensions]} {
        return $path
    }
    lassign $dimensions original_width original_height
    # Do not enlarge images or decode exceptionally large bitmaps.
    if {$original_width <= $width || $original_height <= 0
        || $original_width * wide($original_height) > 40000000} {
        return $path
    }
    set directory [directory]
    set fingerprint [ns_sha1 [list v1 $identity [file normalize $path] \
                                 [file mtime $path] [file size $path] $width $type]]
    set cached [file join $directory $fingerprint.$type]
    if {[file readable $cached]} { return $cached }
    set binary [::image::convert_binary]
    if {$binary eq ""} { return $path }

    # Bound concurrent conversions; a busy converter falls back to the original.
    set mutex [nsv_get attachments_image_cache mutex]
    if {[ns_mutex trylock $mutex] != 0} { return $path }
    set temporary ""
    try {
        if {[file readable $cached]} { return $cached }
        file mkdir $directory
        file attributes $directory -permissions 0700
        set channel [file tempfile temporary [file join $directory converting-]]
        close $channel
        # Explicit output format: cache temporary files have no extension.
        # Coalesce/optimize preserves animated GIFs rather than keeping one frame.
        exec $binary -limit thread 1 -limit memory 64MiB -limit map 128MiB \
            -limit disk 256MiB -limit time 30 $path -coalesce \
            -resize ${width}x> -layers Optimize ${type}:$temporary 2>@1
        if {[ns_imgtype $temporary] ne $type
            || [lindex [ns_imgsize $temporary] 0] != $width} {
            error "Unexpected resized attachment image"
        }
        file attributes $temporary -permissions 0600
        file rename -force -- $temporary $cached
        return $cached
    } on error {message options} {
        ad_log warning "Could not resize attachment image: $message"
        return $path
    } finally {
        if {$temporary ne ""} { catch {file delete -- $temporary} }
        ns_mutex unlock $mutex
    }
}

ad_proc -private ::attachments::image::expire {} {
    Remove disposable variants after seven days; originals remain in the CR.
} {
    set cutoff [expr {[clock seconds] - 7 * 86400}]
    foreach path [glob -nocomplain -directory [directory] -types f *] {
        catch {
            if {[file mtime $path] < $cutoff} { file delete -- $path }
        }
    }
}