Skip to content

Instantly share code, notes, and snippets.

@lazerl0rd
Last active December 4, 2021 17:58
Show Gist options
  • Save lazerl0rd/8be4dc336bd29ef162996b8e1c461d35 to your computer and use it in GitHub Desktop.
Save lazerl0rd/8be4dc336bd29ef162996b8e1c461d35 to your computer and use it in GitHub Desktop.
[Trigger]
Operation = Install
Operation = Upgrade
Type = Path
Target = etc/mime.types
Target = usr/share/webapps/nextcloud/resources/config/mimetypemapping.dist.json
[Action]
Description = Updating Nextcloud's mime type mapping...
When = PostTransaction
Exec = /usr/bin/sudo -u nextcloud /usr/local/bin/nextcloud-mimetypes.sh
Depends = bash
Depends = gawk
Depends = sed
Depends = sudo
#! /usr/bin/env bash
if [[ $USER != nextcloud ]]; then
echo "This script can only be ran by the nextcloud user."
exit 1
fi
wantedMimeTypes=()
readarray -t systemMimeTypes <<< "$(cat "/etc/mime.types" | sed "/^#..*/d" | sed "/^$/d" | sed "/..*\t/"'!'"d" | sed "s/\s\s*/,/" | sed "s/\s/@/g")"
wantedMimeTypes+=("{")
for i in "${systemMimeTypes[@]}"; do
numDups="$(awk -F"@" '{ print NF }' <<< "$i")"
for (( j = 1; j <= $numDups; j++ )); do
fileType="$(echo -n "$i" | awk 'BEGIN { FS=","; OFS=","; } { print $2 }' | awk 'BEGIN { FS="@"; OFS="@"; } { print $'"$j"' }')"
if ! grep -Fq "$(echo -e '"'"$fileType"'":')" "/usr/share/webapps/nextcloud/resources/config/mimetypemapping.dist.json"; then
wantedMimeTypes+=("$(echo -e '\t"'"$fileType"'": ["'"$(echo -n "$i" | awk 'BEGIN { FS=","; OFS=","; } { print $1 }')"'"]'"$([ "$i" != "${systemMimeTypes[-1]}" ] && echo -n ",")")")
fi
done
done
wantedMimeTypes+=("}")
echo -n "" > "/etc/webapps/nextcloud/config/mimetypemapping.json"
for i in "${wantedMimeTypes[@]}"; do
echo "$i" >> "/etc/webapps/nextcloud/config/mimetypemapping.json"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment