Пример #1
0
  public String getDocument(String GUID, OutputStream out) throws DocumentException {

    try {
      GridFS gridFS = new GridFS(dataBase);

      ObjectId key = new ObjectId(GUID);
      GridFSDBFile gridFSDBFile = gridFS.find(key);

      if (gridFSDBFile == null) {
        throw new DocumentException("No existe el documento");
      }
      if (out != null) gridFSDBFile.writeTo(out);
      CommandResult result = dataBase.getLastError();
      if (!result.ok()) {
        throw new DocumentException(result.getErrorMessage());
      }

      DBObject dbObject = gridFSDBFile.getMetaData();
      return JSON.serialize(dbObject);
    } catch (Exception e) {
      log.error("getDocument error:" + e.getMessage());
      e.printStackTrace();
      throw new DocumentException(e.getMessage());
    }
  }
  /**
   * 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));
  }
Пример #3
0
 public static Result getBeerImg(String id) {
   GridFSDBFile img = gfs.find(new ObjectId(id));
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   try {
     img.writeTo(baos);
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   return ok(baos.toByteArray()).as("image/png");
 }
Пример #4
0
  @Test
  public void shouldStoreFileInMultipleChunks() throws Exception {
    final byte[] data = new byte[] {1, 2, 3, 4, 5};

    final GridFSInputFile file = fs.createFile(data);
    file.setChunkSize(3); // chunk size is less than data size in order to get more than one chunk
    file.save();

    final GridFSDBFile result = fs.findOne((ObjectId) file.getId());

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    assertEquals(data.length, result.writeTo(out));

    assertArrayEquals(data, out.toByteArray());
  }
  @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;
  }
Пример #6
0
  public static void main(String[] args) throws Exception {

    MongoClient client = new MongoClient();
    DB db = client.getDB("course");
    FileInputStream inputStream = null;

    GridFS videos =
        new GridFS(db, "video"); // does not create new GridFS it just gives object to manipulate it
    // returns bucket named video
    try {

      inputStream = new FileInputStream("BinarySearch.mp4");

    } catch (FileNotFoundException e) {

      e.printStackTrace();
    }

    GridFSInputFile video = videos.createFile(inputStream, "BinarySearch.mp4");

    BasicDBObject meta = new BasicDBObject("description", "Binary Search");

    ArrayList<String> tags = new ArrayList<String>();
    tags.add("search");
    tags.add("data structures");

    meta.append("tags", tags);

    video.setMetaData(meta);
    video.save();

    System.out.println(video.get("_id"));
    System.out.println("file saved");
    System.out.println("reading file from mongo");

    GridFSDBFile dbFile = videos.findOne(new BasicDBObject("filename", "BinarySearch.mp4"));

    FileOutputStream outputStream = new FileOutputStream("BinarySearch_copy.mp4");

    dbFile.writeTo(outputStream);
  }