diff --git a/src/main/java/org/ntlab/acanthus_server/resources/gallery/StrokesRest.java b/src/main/java/org/ntlab/acanthus_server/resources/gallery/StrokesRest.java index 17ebd68..94c6213 100644 --- a/src/main/java/org/ntlab/acanthus_server/resources/gallery/StrokesRest.java +++ b/src/main/java/org/ntlab/acanthus_server/resources/gallery/StrokesRest.java @@ -1,11 +1,16 @@ package org.ntlab.acanthus_server.resources.gallery; import org.ntlab.acanthus_server.entities.*; +import org.ntlab.acanthus_server.entities.Stroke; import org.ntlab.acanthus_server.models.Gallery; +import org.ntlab.acanthus_server.utils.Base64Decode; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; +import java.io.IOException; import java.util.*; // _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ @@ -16,6 +21,14 @@ private final Gallery gallery = Gallery.getInstance(); private int srstrokeSize=0; private int srstrokeNo=0; + + private ApplicationContext applicationContext; + + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + + // _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ // 1ページの1レイヤーに置ける筆跡情報を全て取得します // 成功時のレスポンス:strokeNo{pen, color, thickness, potions{x, y}} @@ -75,7 +88,7 @@ } // 画像URLを返す - @Path("/{aid}/pageMap/image") + @Path("/{aid}/pageMap/{pid}/image") @GET @Produces(MediaType.APPLICATION_JSON) public HashMap getPageUrls(@PathParam("aid") Integer aid) { @@ -91,6 +104,40 @@ return pageUrl; } + + + // _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ + // スクショで撮った画像をサーバ内の指定フォルダーにおく + // @PathParam Integer aid 作品のID + // @PathParam Integer pid ページのID + // @FormParam String image Base64 + @Path("/{aid}/pageMap/{pid}/image") + @POST + @Produces(MediaType.APPLICATION_JSON) + public String addImage(@PathParam("aid") Integer aid, @PathParam("pid") Integer pid, @FormParam("image") String image) { + + + String vid = "a" + aid.toString() + pid.toString(); + String path1 = ""; + try { + path1 = applicationContext.getResource("file:").getFile().getAbsolutePath() + "/apache-tomcat-9.0.10/webapps"; + Base64Decode.saveAsFile(aid, pid, path1, image); + } catch (IOException e) { + e.printStackTrace(); + throw new WebApplicationException(500); + } + + + String url = "http://nitta-lab-www.is.konan-u.ac.jp" + vid; + +/* ArrayList categories = (ArrayList) category; + ScreenImage v = screenimage.createScreenImage(aid, pid, vid, url, categories); + return v;*/ + + return "OK"; + } + + // _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ // レイヤーごとのstrokeをすべて消去 // @PathParam Integer aid 作品のID diff --git a/src/main/java/org/ntlab/acanthus_server/utils/Base64Decode.java b/src/main/java/org/ntlab/acanthus_server/utils/Base64Decode.java new file mode 100644 index 0000000..ae464a0 --- /dev/null +++ b/src/main/java/org/ntlab/acanthus_server/utils/Base64Decode.java @@ -0,0 +1,35 @@ +package org.ntlab.acanthus_server.utils; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Base64; + +public class Base64Decode { + public static String saveAsFile(Integer aid ,Integer pid ,String path1 ,String enimage) throws IOException { + + + //デコードを行う + byte[] dedata = Base64.getDecoder().decode(enimage); + + //ファイルがないときは作成する + String fileName = "a" + aid.toString() + "p" + pid.toString() + ".png"; + String path2 = "/gallery/"+ aid.toString() + pid.toString(); + String path = path1 + path2 + "/" + fileName; + File file = new File(path); + //file.getParentFile().mkdirs(); + file.createNewFile(); + + //ファイルに画像データを書き込む + FileOutputStream file1 = new FileOutputStream(file, false); + BufferedOutputStream bf = new BufferedOutputStream(file1); + bf.write(dedata, 0, dedata.length); + + //ファイルを閉じる + bf.close(); + + //セーブしたファイルの相対パスを文字列として返す + return path2; + } +}