Fix not creating directory when uploading file
This commit is contained in:
		@ -22,6 +22,7 @@ import java.awt.image.BufferedImage;
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.nio.file.Files;
 | 
			
		||||
import java.nio.file.Path;
 | 
			
		||||
import java.nio.file.Paths;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
@ -171,15 +172,22 @@ public class InternArticleController {
 | 
			
		||||
				if (imageID.isPresent()) {
 | 
			
		||||
					article.image = imageRepository.findImageById(imageID.get()); // add existing img to article
 | 
			
		||||
				} else {
 | 
			
		||||
 | 
			
		||||
					Path targetPath = Paths.get("./data/img/");
 | 
			
		||||
					if (Files.notExists(targetPath)) {
 | 
			
		||||
						Files.createDirectories(targetPath);
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					// write new img file to disk
 | 
			
		||||
					Files.newOutputStream(Paths.get("./data/img/", fileName)).write(imgFile.getBytes());
 | 
			
		||||
					Files.newOutputStream(Paths.get(targetPath.toString(), fileName)).write(imgFile.getBytes());
 | 
			
		||||
					// create new img
 | 
			
		||||
					Image newImage = new Image();
 | 
			
		||||
					newImage.path = "./data/img/" + fileName; // set new file to new img
 | 
			
		||||
					imageRepository.save(newImage); // save new img
 | 
			
		||||
					article.image = newImage; // set new img to article
 | 
			
		||||
				}
 | 
			
		||||
			} catch (Exception e) {
 | 
			
		||||
			} catch (IOException e) {
 | 
			
		||||
				e.printStackTrace();
 | 
			
		||||
				setDefaultImage(article); // if upload failed, reset to default img
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user