Search in sources :

Example 1 with PageRequest

use of com.xenoage.zong.webserver.model.requests.PageRequest in project Zong by Xenoage.

the class PageAction method performTry.

@Override
public boolean performTry(Request request, Webserver server, HttpServletResponse response) throws SQLException, IOException {
    PageRequest pageRequest = getAs(PageRequest.class, request);
    Connection db = server.getDBConnection();
    // get ID and number of pages of document
    PreparedStatement stmtID = stmt(db, "SELECT id, pages FROM docs WHERE public_id = ?", pageRequest.id);
    ResultSet resID = stmtID.executeQuery();
    if (!resID.next()) {
        stmtID.close();
        return false;
    }
    int docID = resID.getInt(1);
    int pages = resID.getInt(2);
    stmtID.close();
    // unknown page?
    if (pageRequest.page < 0 || pageRequest.page >= pages) {
        throw new IOException("unknown page");
    }
    // load size of first page
    // if it does not exist (yet), wait a little bit and try again
    PreparedStatement stmtFirstPageSize = stmt(db, "SELECT width, height FROM pageinfos WHERE doc_id = ? AND page = 0", docID);
    ResultSet resFirstPageSize = stmtFirstPageSize.executeQuery();
    if (!resFirstPageSize.next()) {
        stmtFirstPageSize.close();
        return false;
    }
    Size2f firstPageSize = new Size2f(resFirstPageSize.getFloat(1), resFirstPageSize.getFloat(2));
    stmtFirstPageSize.close();
    // unknown scaling?
    int scaling = pageRequest.scaling.convertTo10000(firstPageSize);
    boolean scalingExists = (ScaledPage.fromDB(db, docID, 0, scaling) != null);
    if (!scalingExists) {
        throw new IOException("unknown scaling");
    }
    // deliver page image
    PreparedStatement stmtImage = stmt(db, "SELECT image FROM pages WHERE doc_id = ?" + " AND page = ? AND scaling = ?", docID, pageRequest.page, scaling);
    ResultSet resImage = stmtImage.executeQuery();
    if (!resImage.next()) {
        stmtImage.close();
        return false;
    }
    Blob blob = resImage.getBlob(1);
    byte[] imageData = blob.getBytes(1, (int) blob.length());
    stmtImage.close();
    response.setHeader("Content-Type", "image/png");
    response.getOutputStream().write(imageData);
    // update access time
    PreparedStatement stmtTime = stmt(db, "UPDATE docs SET last_access = ? WHERE id = ?", unixTime(), docID);
    stmtTime.executeUpdate();
    stmtTime.close();
    return true;
}
Also used : PageRequest(com.xenoage.zong.webserver.model.requests.PageRequest) Blob(java.sql.Blob) Size2f(com.xenoage.utils.math.geom.Size2f) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Aggregations

Size2f (com.xenoage.utils.math.geom.Size2f)1 PageRequest (com.xenoage.zong.webserver.model.requests.PageRequest)1 IOException (java.io.IOException)1 Blob (java.sql.Blob)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1