summaryrefslogtreecommitdiff
path: root/src/lib/image.source.sh
blob: f7cb715170a5b4446a809acda983884c86978ea2 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
cleanphotos() {
    local basename
    local photo
    local sub

    while IFS= read -r photo; do
        basename=$(basename "$photo")

        if [[ -f "$INCOMING_DIR/$basename" ]] \
            && is_supported_image_file "$basename"; then
            continue
        fi

        log_info "Cleaning up $(_display_path "$photo")"
        for sub in thumbs blurs photos; do
            if [ -f "$DIST_DIR/$sub/$basename" ]; then
                rm -f "$DIST_DIR/$sub/$basename"
                log_info "removed '$(_display_path "$DIST_DIR/$sub/$basename")'"
            fi
        done
    done < <(find "$DIST_DIR/photos" -maxdepth 1 -type f)
}

is_supported_image_file() {
    local -r file="$1"; shift
    local extension

    if [[ "$file" != *.* ]]; then
        return 1
    fi

    extension="${file##*.}"
    extension="${extension,,}"

    case "$extension" in
        gif|jpeg|jpg|png|webp)
            return 0
            ;;
        *)
            return 1
            ;;
    esac
}

incoming_image_files() {
    local file

    while IFS= read -r file; do
        if is_supported_image_file "$file"; then
            printf '%s\n' "$file"
        fi
    done < <(find "$INCOMING_DIR" -maxdepth 1 -type f -printf '%f\n') \
        | sort
}

warn_unsupported_incoming_files() {
    local file

    while IFS= read -r file; do
        if ! is_supported_image_file "$file"; then
            log_warning "Ignoring unsupported incoming file: $file"
        fi
    done < <(find "$INCOMING_DIR" -maxdepth 1 -type f -printf '%f\n' | sort)
}

scalephotos() {
    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
        scale_photo "$photo" &
        image_job_pids+=("$!")
        # Read through a nameref in the job-pool helpers.
        # shellcheck disable=SC2034
        image_job_labels["$!"]="image job for photo $photo"
    done < <(incoming_image_files)

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

scale_photo() {
    local -r photo="$1"; shift
    local destphoto
    local dirname

    destphoto="$DIST_DIR/photos/$photo"
    dirname=$(dirname "$destphoto")
    mkdir -p "$dirname"

    if [ -f "$destphoto" ]; then
        log_verbose "Skipped existing photo $(_display_path "$destphoto")"
        return
    fi

    log_info "Processing $photo to $(_display_path "$destphoto")"
    if [ -n "$HEIGHT" ]; then
        # Scale down size.
        imagemagick \
            "$INCOMING_DIR/$photo" \
            -auto-orient \
            -geometry "x${HEIGHT}>" \
            "$destphoto"
    else
        # Keep original size.
        imagemagick \
            "$INCOMING_DIR/$photo" \
            -auto-orient \
            "$destphoto"
    fi
}