Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

ImageService is an interface. Persisting and retrieving images is the responsibility of any implementation of the ImageService interface. By using an interface, if we decide to persist this information in a different way than what we've done in the past, we just create a different service implementation and use that instead.

unmigrated-wiki-markup
Panel
Code Block

 public interface ImageService
 {
     public byte[] getImage(ImageEntry imageEntry)
        throws IOException;
     
     public boolean isImageAvailable(ImageEntry imageEntry);
     
     public Date getLastModifyTime(ImageEntry imageEntry);
     
     public byte[] getThumbnail(ImageEntry imageEntry)
        throws IOException;
 
     @Transactional
     public void save(ImageEntry imageEntry, InputStream imageStream)
        throws IOException;
 }

We have a special implementation of the ImageService interface called ImageServiceImpl. This implementation stores the image and a scaled thumbnail as files on the file system in a special folder. The name of the files are generated via random UUIDs. This file name along with other information, such as the file name on the client's computer and the content type is saved into a database record. Retrieving an image or its thumbnail is done by looking up the record in the database via its unique ID, retrieving the full path to the file system file, reading data from the file, and returning the data as a byte array. It could also be done by returning an InputStream instead of the byte array.

...