package com.ntlab.irisserver.resources;
import com.ntlab.irisserver.utils.Base64Decode;
import org.springframework.context.ApplicationContextAware;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Component;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
@Component
@Path("/rooms")
public class DrawingsRest implements ApplicationContextAware {
private org.springframework.context.ApplicationContext applicationContext;
public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
// お絵描きの終了判定
@GET
@Path("/{rid}/game/drawings")
public String getDrawings(@PathParam("rid") String rid) {
// 絵のデータ取得
return null;
}
// 絵のデータ取得(パスを渡せば良い)
@GET
@Path("/{rid}/game/drawings/{dno}")
@Produces(MediaType.APPLICATION_JSON)
public String getDrawing(@PathParam("rid") String rid, @PathParam("dno") int dno ) {
String path = "";
String path1 = "";
String fileName = "";
try {
path1 = applicationContext.getResource("file:").getFile().getAbsolutePath() + "/apache-tomcat-9.0.10/webapps/irisdata";
fileName= rid + "-" + dno + ".png";
path = path1 + fileName;
System.out.println("生成されたパス:" + path.toString());
} catch (IOException e) {
e.printStackTrace();
throw new WebApplicationException(500);
}
return path;
}
// 描いた絵を送る
@PUT
@Path("/{rid}/game/drawings/{dno}")
@Produces(MediaType.APPLICATION_JSON)
public String putDrawing(@PathParam("rid") String rid, @PathParam("dno") int dno, @FormParam("drawing") String drawing ) {
String path1 = "";
String path = "";
try {
path1 = applicationContext.getResource("file:").getFile().getAbsolutePath() + "/apache-tomcat-9.0.10/webapps";
System.out.println("保存前のディレクトリのパス:" + path1.toString());
System.out.println("クライアントから届いたBASE64の文字列"+ drawing.toString());
// utilsのBase64Decodeにデコードしてもらって、そのついでに保存してもらう
path = Base64Decode.saveAsFile(rid, dno, path1, drawing);
} catch (IOException e) {
e.printStackTrace();
throw new WebApplicationException(500);
}
System.out.println("画像デーをが追加しました。これがパス:" + path);
return "画像保存完了";
}
}