/**
   * Update the GridFSDBFile in the associated DB with the key/values in updateKeys
   *
   * @param updateKeys Map of new tag data
   * @param file GridFSDBFile to update with tag data
   * @param db
   * @param songId ID of Song to update with tag data
   * @return
   */
  public static boolean updateFile(
      Map<String, String> updateKeys,
      GridFSDBFile file,
      DB db,
      ObjectId songId) { // TODO updateKeys?
    File audioFile = null;
    try {
      audioFile = File.createTempFile("tmp", ".mp3");
    } catch (IOException e) {
      log.error("tmp file not created", e);
    }

    audioFile.deleteOnExit();
    AudioFile f = null;
    ObjectId id = (ObjectId) file.getId();
    ObjectId oid = null;
    try {
      file.writeTo(audioFile);
      f = AudioFileIO.read(audioFile);
      Tag tag = f.getTagOrCreateAndSetDefault();
      DBObject q = new BasicDBObject("_id", songId);
      DBObject o = new BasicDBObject("$set", new BasicDBObject(updateKeys));

      if (updateKeys.get("artist") != null) {
        tag.setField(FieldKey.ARTIST, updateKeys.get("artist"));
      }
      if (updateKeys.get("album") != null) {
        tag.setField(FieldKey.ALBUM, updateKeys.get("album"));
      }
      if (updateKeys.get("title") != null) {
        tag.setField(FieldKey.TITLE, updateKeys.get("title"));
      }
      if (updateKeys.get("track") != null) {
        tag.setField(FieldKey.TRACK, updateKeys.get("track"));
      }
      if (updateKeys.get("year") != null) {
        tag.setField(FieldKey.YEAR, updateKeys.get("year"));
      }
      AudioFileIO.write(f);
      GridFS myFS = new GridFS(db);
      myFS.remove(id);
      GridFSInputFile inputFile =
          putSongFileInDB(f.getFile(), db, file.getContentType(), file.getFilename(), id);
      oid = (ObjectId) inputFile.getId();
      if (oid.equals(id)) {
        db.getCollection("songs").update(q, o);
      }
    } catch (KeyNotFoundException knfe) {
      log.error("key not found", knfe);
    } catch (FieldDataInvalidException fdie) {
      log.error("tried to set field with invalid value", fdie);
    } catch (Exception e) {
      log.error("error reading/writing file", e);
    }
    return (oid.equals(id));
  }
Ejemplo n.º 2
0
 private void writeResponse(GridFSDBFile file, HttpServletResponse response) throws IOException {
   if (file != null) {
     byte[] data = IOUtils.toByteArray(file.getInputStream());
     response.setContentType(file.getContentType());
     response.setContentLength((int) file.getLength());
     response.getOutputStream().write(data);
     response.getOutputStream().flush();
   } else {
     response.setStatus(HttpStatus.NOT_FOUND.value());
   }
 }
Ejemplo n.º 3
0
 @GET
 @Path("/{id}")
 public javax.ws.rs.core.Response getAttachment(@PathParam("id") final String id)
     throws IOException {
   final DB db = this.client.getDB("grid");
   final GridFS gridFS = new GridFS(db);
   final GridFSDBFile file = gridFS.findOne(id);
   // log.info(file);
   if (file == null) {
     throw new WebApplicationException(404);
   }
   return Response.ok(
           org.apache.commons.io.IOUtils.toByteArray(file.getInputStream()), file.getContentType())
       .build();
 }
Ejemplo n.º 4
0
 @GET
 @Path("{objectid}")
 public Response getImage(
     @PathParam("objectid") String objectId, @HeaderParam("If-Modified-Since") String modified) {
   GridFSDBFile mongoFile = imageRepository.getImage(objectId);
   if (mongoFile != null) {
     if (modified != null) {
       if (new Date(modified).before(mongoFile.getUploadDate())) {
         return Response.status(Status.NOT_MODIFIED).build();
       }
     }
     return Response.ok(mongoFile.getInputStream(), mongoFile.getContentType())
         .lastModified(mongoFile.getUploadDate())
         .build();
   }
   return Response.status(Status.NOT_FOUND).build();
 }
  @Override
  protected UrnObject get(String objectKey) {
    Query query = new Query(Criteria.where("filename").is(objectKey));
    GridFSDBFile file = this.template.findOne(query);
    if (file == null) {
      return null;
    }

    // check for expiry
    Long expiry = (Long) file.get(FIELD_EXPIRY);
    if (expiry != null) {
      if (expiry.longValue() < System.currentTimeMillis()) {
        this.remove(objectKey);
        return null;
      }
    }

    byte[] bytes = null;
    try {
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      file.writeTo(stream);
      stream.close();
      bytes = stream.toByteArray();
    } catch (IOException e) {
      LOGGER.error("Error reading file from mongo database for key: " + objectKey, e);
      return null;
    }

    UrnObject urnObject = new UrnObject(objectKey, bytes);

    // now the metadata
    if (expiry != null) {
      urnObject.expiry = expiry.longValue();
    }

    urnObject.name = (String) file.get(FIELD_NAME);
    urnObject.mime = file.getContentType();
    urnObject.stored = file.getUploadDate().getTime();

    // return the object
    return urnObject;
  }