Java tutorial
/* * * Springstrap * * @author Jan Philipp Knller <info@pksoftware.de> * * Homepage: http://ui5strap.com/springstrap * * Copyright (c) 2013-2014 Jan Philipp Knller <info@pksoftware.de> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * Released under Apache2 license: http://www.apache.org/licenses/LICENSE-2.0.txt * */ package de.pksoftware.springstrap.cms.service; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.codec.binary.Base64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import de.pksoftware.springstrap.cms.entity.CmsModel; import de.pksoftware.springstrap.cms.repository.ICmsModelRepository; import de.pksoftware.springstrap.fs.service.FileSystemBucket; import de.pksoftware.springstrap.fs.service.IFileSystemService; import de.pksoftware.springstrap.fs.service.ItemVisibility; import de.pksoftware.springstrap.fs.service.SitePath; @Service public class CmsService { public static final String BUCKET_NAME = "springstrap-cms"; private static final Logger logger = LoggerFactory.getLogger(CmsService.class); protected static FileSystemBucket cmsBucket = new FileSystemBucket(BUCKET_NAME); @Autowired private ICmsModelRepository cmsModelRepository; @Autowired private IFileSystemService fileSystemService; public void saveModel(CmsModel cmsModel) { cmsModelRepository.save(cmsModel); } public CmsModel loadModel(String path) { return cmsModelRepository.findByModelName(path); } private String getFileExtensionFromMime(String mimeType) { String fileExtension = null; if (mimeType.equals("image/gif")) { fileExtension = "gif"; } else if (mimeType.equals("image/png")) { fileExtension = "png"; } else if (mimeType.equals("image/jpg") || mimeType.equals("image/jpeg")) { fileExtension = "jpg"; } return fileExtension; } public String convertBase64Images(String modelName, String path, String value) { Pattern base64ImagePattern = Pattern.compile( "\\<img.+src\\=\\\"data\\:(image\\/[a-z]+)\\;base64\\,([a-zA-Z0-9\\+\\/]+[\\=]*)\\\"[^\\>]*\\>"); Pattern fileNamePattern = Pattern.compile("data\\-filename\\=\\\"([\\w\\.\\-\\(\\)]+)\\\""); Matcher base64ImageMatcher = base64ImagePattern.matcher(value); if (base64ImageMatcher.find()) { String imgTag = base64ImageMatcher.group(0); logger.info("Found img tag: {}", imgTag); String mimeType = base64ImageMatcher.group(1); String base64Image = base64ImageMatcher.group(2); logger.info("Found {} Image: {}", mimeType, base64Image); Matcher fileNameMatcher = fileNamePattern.matcher(imgTag); String fileName = null; if (fileNameMatcher.find()) { fileName = fileNameMatcher.group(1); logger.info("Filename is: {}", fileName); } if (null == fileName) { String fileExtension = getFileExtensionFromMime(mimeType); UUID fileNameUUID = UUID.randomUUID(); fileName = fileNameUUID.toString() + "." + fileExtension; } logger.info("File name is: {}", fileName); String imagePath = "models/" + modelName + "/" + fileName; String finalImagePath = imagePath; /* boolean fileExists = true; int i = 0; while(fileExists){ if(i > 0){ logger.info("File already exists: {}", finalImagePath); finalImagePath = "(" + i + ")" + imagePath; } try { fileExists = fileSystemService.existPublicFile(BUCKET_NAME, finalImagePath); i++; } catch (InvalidPathException | InvalidBucketException e) { break; } } */ byte[] data = Base64.decodeBase64(base64Image); try { SitePath imageSitePath = new SitePath(finalImagePath); fileSystemService.writeFile(ItemVisibility.PUBLIC, cmsBucket, imageSitePath, data, true, true); value = base64ImageMatcher.replaceFirst("<img src=\"" + fileSystemService.getItemURL(ItemVisibility.PUBLIC, cmsBucket, imageSitePath) + "\" />"); } catch (Exception ex) { logger.error("Could not write file: " + ex.getMessage()); } } return value; } }