Original link: https://blog.kelu.org/tech/2023/08/21/automatically-uploads-local-markdown-images-to-minio-script.html
I basically use typora, a markdown tool, to write blogs. When inserting a picture, it will automatically generate a picture backup locally. In this article, I will automatically replace the image path in the article, change it to the cloud path, and automatically upload the image to the cloud. The service I upload to the cloud is minio. Also, this script is running on a Mac. For other systems, some fine-tuning may be required.
The requirements are as follows:
For example, I finished this article on August 4: 2023-08-04-displaying-images-in-the-linux-command-line.md
I need to put in the file
- (2023-08-04-displaying-images-in-the-linux-command-line.assets/image-20230816103829510.jpg)
- (2023-08-04-displaying-images-in-the-linux-command-line.assets/image-20230816103916037.jpg)
Change these two contents to:
- (https://ift.tt/vdDNImg)
- (https://ift.tt/vdDNImg)
The following is replace.sh
, just execute the following command:
./replace.sh 2023-08-04-displaying-images-in-the-linux-command-line.md
The core commands are these two:
- change folder to url
- change png to jpg
In addition, I have completed an additional function to delete consecutive blank lines into one blank line:
sed '/^$/N;/\n$/D' " ${ FILE } " > $temp_file mv $temp_file " ${ FILE } "
For the commands uploaded to minio, refer to the minio official document MinIO Client :
mc cp " $IMAGE " "myminio/blog/ ${ DATE_PART } / ${ IMAGE_NAME } "
In addition, I also logged in to the server and converted the uploaded png images to jpg. The specific implementation of png2jpg is as follows, which is not reflected in the script. You can refer to my article “Batch Convert Image Format PNG to jpg” :
png2jpg () { if ls --color = tty * .png > /dev/null 2>&1 then ls --color = tty -1 * .png | xargs -n 1 bash -c 'echo $0 && convert "$0" -background white -flatten -alpha off "${0%.png}.jpg"' rm -rf * .png fi if ls --color = tty * .PNG > /dev/null 2>&1 then ls --color = tty -1 * .PNG | xargs -n 1 bash -c 'echo $0 && convert "$0" -background white -flatten -alpha off "${0%.PNG}.jpg"' rm -rf * .PNG fi }
The final complete script is as follows:
#!/bin/bash if [[ $# -ne 1 ]] ; then echo "用法: $0 <文件名>" exit 1 fi FILE = " $1 " ASSETS_DIR = $( echo " $FILE " | sed 's/\.md$/.assets/' ) DATE_PART = $( echo " $FILE " | cut -d '-' -f1-2 | tr '-' '/' ) NEW_URL = "https://cdn.kelu.org/blog/ ${ DATE_PART } " rand_name = " $( openssl rand -hex 8 ) " temp_file = $( mktemp -t rand_name )
sed '/^$/N;/\n$/D' " ${ FILE } " > $temp_file mv $temp_file " ${ FILE } " if [ -d " $ASSETS_DIR " ] ; then for IMAGE in " $ASSETS_DIR " / * ; do IMAGE_NAME = $( basename " $IMAGE " ) mc cp " $IMAGE " "myminio/blog/ ${ DATE_PART } / ${ IMAGE_NAME } " done echo "图片拷贝完成!" ssh sh1 << EOF source .sh.alias cd cdn/blog/ ${ DATE_PART } png2jpg EOF fi
In addition, I also wrote a script batch_replace.sh
to execute this replace.sh
in batches:
#!/bin/bash for FILE in * .md ; do { if [ -f " $FILE " ] ; then ./replace.sh " $FILE " fi } & done wait
This article is transferred from: https://blog.kelu.org/tech/2023/08/21/automatically-uploads-local-markdown-images-to-minio-script.html
This site is only for collection, and the copyright belongs to the original author.