Newer
Older
IrisServer / src / main / java / com / ntlab / irisserver / resources / DrawingsRest.java
package com.ntlab.irisserver.resources;

import com.ntlab.irisserver.entities.Drawing;
import com.ntlab.irisserver.entities.Game;
import com.ntlab.irisserver.entities.Room;
import com.ntlab.irisserver.models.RoomManager;
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 Drawing getDrawing(@PathParam("rid") String rid, @PathParam("dno") int dno ) {

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Drawing drawing = game.getDrawing(dno);

        return drawing;

    }

    // 描いた絵を送る
    @PUT
    @Path("/{rid}/game/drawings/{dno}")
    @Produces(MediaType.APPLICATION_JSON)
    public String putDrawing(@PathParam("rid") String rid, @PathParam("dno") int dno, @FormParam("drawing") String drawingStr ) {
        String pathDirectory = "";
        String path = "";

        try {
            pathDirectory = applicationContext.getResource("file:").getFile().getAbsolutePath() + "/apache-tomcat-9.0.10/webapps";
            System.out.println("保存前のディレクトリのパス:" + pathDirectory.toString());
            System.out.println("クライアントから届いたBASE64の文字列"+ drawingStr.toString());
            // utilsのBase64Decodeにデコードしてもらって、そのついでに保存してもらう
            path = Base64Decode.saveAsFile(rid, dno, pathDirectory, drawingStr);
            Drawing drawing = new Drawing(path);

            RoomManager rm = RoomManager.getInstance();
            Room room = rm.getRoom(rid);
            Game game = room.getGame();
            game.putDrawing(dno, drawing);

        } catch (IOException e) {
            e.printStackTrace();
            throw new WebApplicationException(500);
        }

        System.out.println("画像データの追加完了。これがパス:" + path);

        return "画像保存完了";
    }

}