diff --git a/src/main/java/org/ntlab/tampoposerver/resources/UsersResource.java b/src/main/java/org/ntlab/tampoposerver/resources/UsersResource.java index 2a707ba..84a52b4 100644 --- a/src/main/java/org/ntlab/tampoposerver/resources/UsersResource.java +++ b/src/main/java/org/ntlab/tampoposerver/resources/UsersResource.java @@ -8,9 +8,13 @@ import org.ntlab.tampoposerver.repositories.UserRepository; import org.ntlab.tampoposerver.models.User; import org.ntlab.tampoposerver.services.FriendService; +import org.ntlab.tampoposerver.utils.Base64Decode; +import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -29,7 +33,7 @@ コンストラクタを作る import class コンストラクタの上に@Autowired これを入れるとspring bootが管理しているuserRepositoryのインスタンスを渡してくれる -Repositoru側の説明 Repositoryをつけるとspring bootがインスタンスを1個だけ勝手に作ってくれる シングルトン +Repository側の説明 Repositoryをつけるとspring bootがインスタンスを1個だけ勝手に作ってくれる シングルトン 変数名は先頭小文字 未コミットの編集→スタッシュで書き換えてしまったコードを退避させることができる @@ -40,7 +44,12 @@ @Path("/users") @Component -public class UsersResource { +public class UsersResource implements ApplicationContextAware { + private org.springframework.context.ApplicationContext applicationContext; + + public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } private final UserRepository userRepository; @@ -396,6 +405,9 @@ } User user = userRepository.getUser(userId); + String pathDirectory = ""; + String iconPath = ""; + if (user == null) { throw new WebApplicationException( Response.status(Response.Status.NOT_FOUND) @@ -413,7 +425,15 @@ } //アイコンを変更する - user.setIcon(newIcon); + try { + pathDirectory = applicationContext.getResource("file:").getFile().getAbsolutePath() + "/apache-tomcat-9.0.10/webapps/tampopo-data"; + iconPath = Base64Decode.saveIcon(userId, pathDirectory, newIcon); + user.setIcon(iconPath); + } catch (IOException e){ + e.printStackTrace(); + throw new WebApplicationException(500); + } + return Response.ok(user.getIcon(), MediaType.TEXT_PLAIN).build().toString(); } diff --git a/src/main/java/org/ntlab/tampoposerver/utils/Base64Decode.java b/src/main/java/org/ntlab/tampoposerver/utils/Base64Decode.java new file mode 100644 index 0000000..c010427 --- /dev/null +++ b/src/main/java/org/ntlab/tampoposerver/utils/Base64Decode.java @@ -0,0 +1,43 @@ +package org.ntlab.tampoposerver.utils; + +import java.io.*; +import java.util.Base64; + +public class Base64Decode { + + //pathの位置にenImageをdecodeしたものを保存 + private static void saveAsFile(String path, String enImage) throws IOException{ + File file = new File(path); //pathの位置にfile作成準備 + + //enImageがnullならfile削除 + if(enImage == null) { + file.delete(); + return; + } + + //enImage(文字列)をバイナリデータに変換 + byte[] deData = Base64.getDecoder().decode(enImage); + + //親ディレクトリを作成し実際にfile作成 + file.getParentFile().mkdir(); + file.createNewFile(); + + //書き込み + FileOutputStream fileOS = new FileOutputStream(file, false); + BufferedOutputStream bf = new BufferedOutputStream(fileOS); + bf.write(deData, 0, deData.length); + + bf.close(); + } + + //{path1}/icon{id}.jpg に enImageをdecodeしたものを保存 + public static String saveIcon(String id, String path1, String enImage) throws IOException { + String fileName = "icon" + id + ".jpg"; + String path = path1 + "/" + fileName; + + saveAsFile(path, enImage); + + return path; + } + +} \ No newline at end of file