private DocumentFile<MemoryType> copy(DocumentFile<MemoryType> df) throws IOException { String s = IOUtils.toString(df.getStream(), df.getEncoding()); df.getStream().close(); df.setStream(IOUtils.toInputStream(s, df.getEncoding())); return new DocumentFile<MemoryType>(df.getDocumentId(), df.getFileName(), IOUtils.toInputStream(s, df.getEncoding()), df.getSavedByStage(), df.getUploadDate()); }
private String getFileUrl(DocumentFile<Local> df) throws UnsupportedEncodingException { return getFileUrl(df.getFileName(), df.getDocumentId()); }
@Override public void write(DocumentFile<MemoryType> df) throws IOException { df.setUploadDate(new Date()); files.add(copy(df)); df.getStream().close(); }
@SuppressWarnings("unchecked") private DocumentFile<T> getDocumentFile(HttpRequest request) throws ParseException, IOException, JsonException { HttpEntity requestEntity = ((HttpEntityEnclosingRequest) request).getEntity(); String requestContent = EntityUtils.toString(requestEntity, "UTF-8"); Object o = SerializationUtils.fromJson(requestContent); if(!(o instanceof Map)) { return null; } Map<String, Object> map = (Map<String, Object>) o; DocumentID<T> id = io.toDocumentId(map.get("documentId")); String fileName = (String) map.get("fileName"); Date d = (Date) map.get("uploadDate"); String encoding = (String) map.get("encoding"); String mimetype = (String) map.get("mimetype"); String savedByStage = (String) map.get("savedByStage"); InputStream is; if(encoding == null) { is = new ByteArrayInputStream(Base64.decodeBase64(((String)map.get("stream")).getBytes("UTF-8"))); } else { is = new ByteArrayInputStream(Base64.decodeBase64(((String)map.get("stream")).getBytes(encoding))); } DocumentFile<T> df = new DocumentFile<T>(id, fileName, is, savedByStage, d); df.setEncoding(encoding); df.setMimetype(mimetype); return df; }
public void postDocuments(int numberToPost) throws JsonException, IOException, HttpException, URISyntaxException { RemotePipeline rp = new HttpRemotePipeline("insertStage"); for(int i=0; i<numberToPost; i++) { rp.saveFull(LocalDocumentFactory.getRandomStringDocument("in", "id")); RemotePipeline rp2 = new HttpRemotePipeline("fileAdder"); LocalDocument ld = rp2.getDocument(new LocalQuery()); File f = getFile(); FileInputStream fis = new FileInputStream(f); DocumentFile<Local> df = new DocumentFile<Local>(ld.getID(), f.getName(), fis); df.setEncoding(new InputStreamReader(df.getStream()).getEncoding()); df.setMimetype("application/msword"); rp2.saveFile(df); } }
QueryBuilder qb = QueryBuilder.start() .put(DOCUMENT_KEY) .is(df.getDocumentId().getID()) .and(FILENAME_KEY) .is(df.getFileName()); documentfs.remove(qb.get()); GridFSInputFile input = documentfs.createFile(df.getStream(), df.getFileName()); input.put(DOCUMENT_KEY, df.getDocumentId().getID()); input.put(ENCODING_KEY, df.getEncoding()); input.put(MIMETYPE_KEY, df.getMimetype());
@Override public void process(LocalDocument doc) throws IOException { List<String> files = doc.getFileNames(); for (String fileName : files) { DocumentFile<Local> df = doc.getFile(fileName); File dir = new File(path + "/"); dir.mkdirs(); File file = null; if (prefix) { file = new File(path + "/" + doc.getContentField("id") + "_" + fileName); } else { file = new File(path + "/" + fileName); } file.createNewFile(); FileOutputStream fw = new FileOutputStream(file); InputStream in = df.getStream(); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { fw.write(buffer, 0, len); } fw.close(); } }
private boolean writeAttachments(DatabaseDocument<MemoryType> d, List<DocumentFile<MemoryType>> attachments) { for(DocumentFile<MemoryType> attachment: attachments) { attachment.setDocumentId(d.getID()); try { write(attachment); } catch (IOException e) { logger.error( String.format( "Exception while writing filename:%s for id:%s", attachment.getFileName(), d.getID() ), e ); return false; } } return true; }
private void handleSaveFile(HttpRequest request, HttpResponse response) { try { DocumentFile<T> df = getDocumentFile(request); if (df == null) { HttpResponseWriter.printBadRequestContent(response); } DatabaseDocument<T> md = io.getDocumentById(df.getDocumentId()); if (md == null) { HttpResponseWriter.printNoDocument(response); return; } io.write(df); HttpResponseWriter.printOk(response); } catch (Exception e) { logger.error("An error occurred during file save", e); HttpResponseWriter.printUnhandledException(response, e); } }
DocumentFile<Local> df = new DocumentFile<Local>(docid, fileName, is, savedByStage, d); df.setEncoding(encoding); df.setMimetype(mimetype);
@Override public boolean deleteDocumentFile(DatabaseDocument<MemoryType> d, String fileName) { for (DocumentFile<MemoryType> f : files) { if (f.getDocumentId().equals(d.getID()) && f.getFileName().equals(fileName)) { files.remove(f); return true; } } return false; }
@Override public void process(LocalDocument doc) throws TikaException, SAXException, IOException { List<String> files = doc.getFileNames(); DocumentParserHelper documentParserHelper = new DocumentParserHelper(addMetaData, addLanguage); for (String fileName : files) { DocumentFile<Local> df = doc.getFile(fileName); String prefix = fileName.replace('.', '_') + "_"; InputStreamParser inputStreamParser = new InputStreamParser(parser); ParsedData parsedData = inputStreamParser.parse(df.getStream()); documentParserHelper.addParsedDataToDocument(parsedData, doc, prefix); } }
private boolean writeAttachments(DatabaseDocument<MongoType> d, List<DocumentFile<MongoType>> attachments) { for(DocumentFile<MongoType> attachment: attachments) { attachment.setDocumentId(d.getID()); try { write(attachment); } catch (IOException e) { logger.error( String.format( "Exception while writing filename:%s for id:%s", attachment.getFileName(), d.getID() ), e ); return false; } } return true; }
private DocumentFile<MongoType> getDocumentFile(DocumentID<MongoType> id, String filename) { DBObject query = QueryBuilder.start(DOCUMENT_KEY).is(id.getID()).and(FILENAME_KEY).is(filename).get(); GridFSDBFile file = documentfs.findOne(query); if(file==null) { return null; } DocumentFile<MongoType> df = new DocumentFile<MongoType>(id, file.getFilename(), file.getInputStream(), (String)file.get(STAGE_KEY), file.getUploadDate()); if(file.containsField(MIMETYPE_KEY)) { df.setMimetype((String) file.get(MIMETYPE_KEY)); } if(file.containsField(ENCODING_KEY)) { df.setEncoding((String) file.get(ENCODING_KEY)); } return df; }
@Override public List<String> getDocumentFileNames(DatabaseDocument<MemoryType> d) { ArrayList<String> list = new ArrayList<String>(); for (DocumentFile<MemoryType> f : files) { if (f.getDocumentId().equals(d.getID())) { list.add(f.getFileName()); } } return list; }
@Override public DocumentFile<MemoryType> getDocumentFile( DatabaseDocument<MemoryType> d, String fileName) { for (DocumentFile<MemoryType> f : files) { if (f.getDocumentId().getID().equals(d.getID().getID()) && f.getFileName().equals(fileName)) { try { return copy(f); } catch (IOException e) { logger.error("Error copying the streams", e); } } } return null; }