// Takes and image_id, pulls in the image from cache, and goes about
  // encoding it to put it into the database in a transaction.  The last
  // query in the transaction records the image having been stored.
  protected void storeImage(int image_id) throws Exception {
    PhotoImage p = new PhotoImage(image_id);
    SpyDB pdb = getDB();
    Connection db = null;
    Statement st = null;
    Vector v = p.getImage();
    System.err.println(
        "Storer: Got image for " + image_id + " " + v.size() + " lines of data to store.");
    try {
      int i = 0, n = 0;
      db = pdb.getConn();
      db.setAutoCommit(false);
      st = db.createStatement();
      BASE64Encoder base64 = new BASE64Encoder();
      String data = "";

      for (; i < v.size(); i++) {
        String tmp = base64.encodeBuffer((byte[]) v.elementAt(i));
        tmp = tmp.trim();

        if (data.length() < 2048) {
          data += tmp + "\n";
        } else {
          storeQuery(image_id, n, st, data);
          data = tmp;
          n++;
        }
      }
      // OK, this is sick, but another one right now for the spare.
      if (data.length() > 0) {
        System.err.println("Storer:  Storing spare.");
        storeQuery(image_id, n, st, data);
        n++;
      }
      System.err.println("Storer:  Stored " + n + " lines of data for " + image_id + ".");
      st.executeUpdate(
          "update upload_log set stored=datetime(now())\n" + "\twhere photo_id = " + image_id);
      db.commit();
      // Go ahead and generate a thumbnail.
      p.getThumbnail();
    } catch (Exception e) {
      // If anything happens, roll it back.
      if (st != null) {
        try {
          db.rollback();
        } catch (Exception e3) {
          // Nothing
        }
      }
    } finally {
      if (db != null) {
        try {
          db.setAutoCommit(true);
        } catch (Exception e) {
          System.err.println("Error:  " + e);
        }
      }
      pdb.freeDBConn();
    }
  }
  // Get a list of images that have been added, but not yet added into
  // the database.
  protected void doFlush() {
    SpyDB pdb = getDB();
    Vector v = null;
    try {
      Connection db = pdb.getConn();
      Statement st = db.createStatement();
      String query = "select * from upload_log where stored is null";
      ResultSet rs = st.executeQuery(query);
      v = new Vector();
      while (rs.next()) {
        v.addElement(rs.getString("photo_id"));
      }
    } catch (Exception e) {
      // Do nothing, we'll try again later.
    } finally {
      pdb.freeDBConn();
    }

    // Got the vector, now store the actual images.  This is done so
    // that we don't hold the database connection open whlie we're
    // making the list *and* getting another database connection to act
    // on it.
    if (v != null) {
      try {
        for (int i = 0; i < v.size(); i++) {
          String stmp = (String) v.elementAt(i);
          storeImage(Integer.valueOf(stmp).intValue());
        }
      } catch (Exception e) {
        // Don't care, we'll try again soon.
      }
    }
  }
Beispiel #3
0
  public String getViewersOf(Integer photo_id) throws Exception {
    Connection db;
    Statement st;
    String query, out = "";

    try {
      db = getDBConn();
    } catch (Exception e) {
      throw new Exception("Can't get database connection: " + e.getMessage());
    }

    query =
        "select wwwusers.username, log.remote_addr,\n"
            + "   user_agent.user_agent, log.cached, log.ts\n"
            + "  from wwwusers, photo_log log, user_agent\n"
            + "  where log.wwwuser_id = wwwusers.id and\n"
            + "    log.photo_id = "
            + photo_id
            + " and\n"
            + "    user_agent.user_agent_id = log.user_agent\n"
            + "  order by log.ts\n";
    try {
      st = db.createStatement();
      ResultSet rs = st.executeQuery(query);

      Hashtable htmp = new Hashtable();
      htmp.put("PHOTO_ID", photo_id.toString());
      out = PhotoUtil.tokenize(photosession, "log/viewers_top.inc", htmp);

      while (rs.next()) {
        try {
          Hashtable h = new Hashtable();
          h.put("USERNAME", rs.getString(1));
          h.put("REMOTE_ADDR", rs.getString(2));
          h.put("USER_AGENT", rs.getString(3));
          h.put("CACHED", rs.getString(4));
          h.put("TS", rs.getString(5));
          out += PhotoUtil.tokenize(photosession, "log/viewers_match.inc", h);
        } catch (Exception e) {
          log("Error reporting log entry for " + photo_id.toString() + " from " + rs.getString(5));
        }
      }

      out += PhotoUtil.tokenize(photosession, "log/viewers_bottom.inc", new Hashtable());

    } catch (Exception e) {
      throw new Exception(e.getMessage());
    } finally {
      freeDBConn(db);
    }

    return (out);
  }