List of usage examples for com.mongodb.gridfs GridFSInputFile getLength
public long getLength()
From source file:com.ibm.ws.lars.rest.PersistenceBean.java
License:Apache License
/** * @param attachmentContentStream//from w w w . ja va2 s .com * @return */ @Override public AttachmentContentMetadata createAttachmentContent(String name, String contentType, InputStream attachmentContentStream) { // Do not specify a bucket (so the data will be stored in fs.files and fs.chunks) GridFSInputFile gfsFile = gridFS.createFile(attachmentContentStream); ObjectId id = new ObjectId(); gfsFile.setContentType(contentType); gfsFile.setId(id); String filename = id.toString(); gfsFile.setFilename(filename); gfsFile.save(); return new AttachmentContentMetadata(gfsFile.getFilename(), gfsFile.getLength()); }
From source file:com.imaginea.mongodb.services.GridFSServiceImpl.java
License:Apache License
/** * Service implementation for uploading a file to GridFS. * * @param dbName Name of Database/*from w w w . java2 s. c o m*/ * @param bucketName Name of GridFS Bucket * @param formData formDataBodyPart of the uploaded file * @param inputStream inputStream of the uploaded file * @param dbInfo Mongo Db Configuration provided by user to connect to. * @returns Success message with additional file details such as name, size, * download url & deletion url as JSON Array string. */ public JSONArray insertFile(String dbName, String bucketName, String dbInfo, InputStream inputStream, FormDataBodyPart formData) throws DatabaseException, CollectionException, DocumentException, ValidationException { mongoInstance = mongoInstanceProvider.getMongoInstance(); if (dbName == null) { throw new EmptyDatabaseNameException("Database name is null"); } if (dbName.equals("")) { throw new EmptyDatabaseNameException("Database Name Empty"); } if (bucketName == null) { throw new EmptyCollectionNameException("Bucket name is null"); } if (bucketName.equals("")) { throw new EmptyCollectionNameException("Bucket Name Empty"); } JSONArray result = new JSONArray(); FormDataContentDisposition fileData = formData.getFormDataContentDisposition(); try { if (!mongoInstance.getDatabaseNames().contains(dbName)) { throw new UndefinedDatabaseException("DB [" + dbName + "] DOES NOT EXIST"); } GridFS gridFS = new GridFS(mongoInstance.getDB(dbName), bucketName); GridFSInputFile fsInputFile = gridFS.createFile(inputStream, fileData.getFileName()); fsInputFile.setContentType(formData.getMediaType().toString()); fsInputFile.save(); JSONObject obj = new JSONObject(); obj.put("name", fsInputFile.getFilename()); obj.put("size", fsInputFile.getLength()); obj.put("url", String.format("services/%s/%s/gridfs/getfile?id=%s&download=%s&dbInfo=%s&ts=%s", dbName, bucketName, fsInputFile.getId().toString(), false, dbInfo, new Date())); obj.put("delete_url", String.format("services/%s/%s/gridfs/dropfile?id=%s&dbInfo=%s&ts=%s", dbName, bucketName, fsInputFile.getId().toString(), dbInfo, new Date().getTime())); obj.put("delete_type", "GET"); result.put(obj); } catch (Exception e) { CollectionException ce = new CollectionException(ErrorCodes.UPLOAD_FILE_EXCEPTION, "UPLOAD_FILE_EXCEPTION", e.getCause()); throw ce; } return result; }
From source file:org.sakaiproject.nakamura.lite.storage.mongo.GridFSContentHelper.java
License:Apache License
public Map<String, Object> writeBody(String keySpace, String columnFamily, String contentId, String contentBlockId, String streamId, Map<String, Object> content, InputStream in) throws IOException, StorageClientException { String path = getPath(keySpace, columnFamily, contentBlockId); GridFSInputFile file = contentBodies.createFile(in, path); file.save();//from ww w . j a v a 2 s . co m LOGGER.debug("Wrote {} bytes to {} as body of {}:{}:{} stream {} ", new Object[] { file.getLength(), path, keySpace, columnFamily, contentBlockId, streamId }); Map<String, Object> metadata = Maps.newHashMap(); metadata.put(StorageClientUtils.getAltField(InternalContent.LENGTH_FIELD, streamId), file.getLength()); metadata.put(StorageClientUtils.getAltField(InternalContent.BLOCKID_FIELD, streamId), contentBlockId); metadata.put(StorageClientUtils.getAltField(STORE_LOCATION_FIELD, streamId), path); return metadata; }