Profile Image Uploads: On Image File Storage

This is the final post in our series on creating a Profile Image Uploads function. We looked at Spring elements, leveraging Thymeleaf and overall design. Here we'll look specifically at File Storage design.

As we covered earlier, images are saved in 600×600 and 32×32 format in two separate directories. Image filenames are 16-digit alphanumeric User Keys representing the user. Because the files are served over the web by their mime type they have no filename extension.

The storage location is set in an External Properties file and assigned in a Spring ApplicationSettings.java class.

An example of using the User Profile Image Path is shown here to display the User Icon in the Navigation Bar.

Using the File Storage Location in Development

This is the main reason for this post, demonstrating how to access the File Storage location in Development on http://localhost and support urls like the following.

We're going to accomplish this by adding a soft link in our IDE build's /static folder. Here is our soft link shown in IntelliJ.

And here's a bash script to create the link. (You'll find it in the /install/sh folder in the NixMash Spring repo.) Also notice that the script will also optionally remove the soft link. This is important for our app, because we package it as a Fat War with Gradle BootRepackage. Without removing the soft link when packaging the app, the file storage area with all of its files would be included in the application. Definitely something we want to avoid.

#!/bin/bash 

# ---------------------------------------------------------------------- */
# Create path to NixMash Spring File Storage Path - 4/21/2016
# 
# -- Used to point to physical storage location from IDE Build folder
# -- Creates Soft Link in classes /main/static folder to physical location
#
# ---------------------------------------------------------------------- */

# Variables ------------------------------------------------------------- */

# $BUILDPATH - /static folder in IDE build directory for soft link
# $STORAGEPATH - physical location of file storage area
# $LINKNAME - name of soft link to storage area. Same name as storage root

# Set Variables ------------------------------------------------- */

BUILDPATH="/projects/nixmash-spring/mvc/build/classes/main/static"
STORAGEPATH="/sites/nixmashspring/files"
LINKNAME="files"


if [ "$1" != "" ]; then
     if [[ "$1" = "--create" ]]
	then
	   ln -s $STORAGEPATH $BUILDPATH 
	   echo "SOFTLINK TO PHYSICAL FILES LOCATION CREATED"
	elif [[ "$1" = "--rm" ]]
	then
	   rm $BUILDPATH/$LINKNAME
	   echo "SOFTLINK TO PHYSICAL FILES LOCATION REMOVED"
     fi
else
     echo "Use --create to create soft link, --rm to remove"
fi


With this post we hopefully covered everything you need to know to get up and running with the Profile Image Upload function in NixMash Spring. If not, please let me know!

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.

Posted April 26, 2016 09:34 AM EDT

More Like This Post