summaryrefslogtreecommitdiff
path: root/src/lib/image-pipeline.source.sh
blob: 876501baa3914dcd63273a2649079091eb7e3f58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
create_photo_derivatives() {
    local -r photos_dir="$1"; shift
    local -r thumbs_dir="$1"; shift
    local -r blurs_dir="$1"; shift
    local -r photo="$1"; shift
    local dirname
    local height

    if [[ -f "$DIST_DIR/$thumbs_dir/$photo" \
        && -f "$DIST_DIR/$blurs_dir/$photo" ]]; then
        log_verbose "Skipped existing thumb and blur" \
            "$(_display_path "$DIST_DIR/$thumbs_dir/$photo") and" \
            "$(_display_path "$DIST_DIR/$blurs_dir/$photo")"
        return
    fi

    dirname="$DIST_DIR/$thumbs_dir"
    mkdir -p "$dirname"
    log_info "Creating thumb $(_display_path "$DIST_DIR/$thumbs_dir/$photo")"
    # Double the height, as CSS scales images based on boxing too.
    height=$(( THUMBHEIGHT * 2 ))
    imagemagick \
        "$DIST_DIR/$photos_dir/$photo" \
        -geometry "x$height" \
        "$DIST_DIR/$thumbs_dir/$photo"

    dirname="$DIST_DIR/$blurs_dir"
    mkdir -p "$dirname"
    log_info "Creating blur $(_display_path "$DIST_DIR/$blurs_dir/$photo")"
    imagemagick \
        "$DIST_DIR/$thumbs_dir/$photo" \
        -flip \
        -blur 0x8 \
        "$DIST_DIR/$blurs_dir/$photo"
}

create_all_photo_derivatives() {
    local -r photos_dir="$1"; shift
    local -r thumbs_dir="$1"; shift
    local -r blurs_dir="$1"; shift
    local -i failed=0
    local -a image_job_pids=()
    # Passed by name to wait_for_image_job_slot and wait_for_image_jobs.
    # shellcheck disable=SC2034
    local -A image_job_labels=()
    # Passed by name to wait_for_image_job_slot and wait_for_image_jobs.
    # shellcheck disable=SC2034
    local -A image_job_statuses=()
    local photo

    while IFS= read -r photo; do
        wait_for_image_job_slot \
            image_job_pids \
            image_job_statuses \
            image_job_labels \
            failed
        create_photo_derivatives "$photos_dir" "$thumbs_dir" "$blurs_dir" \
            "$photo" &
        image_job_pids+=("$!")
        # Read through a nameref in the job-pool helpers.
        # shellcheck disable=SC2034
        image_job_labels["$!"]="image derivative job for photo $photo"
    done < <(
        find "$DIST_DIR/$photos_dir" -maxdepth 1 -type f -printf '%f\n' \
            | sort
    )

    wait_for_image_jobs \
        image_job_pids \
        image_job_statuses \
        image_job_labels \
        failed
    if (( failed != 0 )); then
        return 1
    fi
}

prepare_generation_photo_assets() {
    warn_unsupported_incoming_files
    mkdir -p "$DIST_DIR/photos"
    cleanphotos
    scalephotos
    create_all_photo_derivatives 'photos' 'thumbs' 'blurs'
}