FileEditSession editSession = editSessions.get(resourceId);
// Create the DTO for the file contents response. We will build it up later in the
// method.
String mimeType = MimeTypes.guessMimeType(currentPath, false);
FileContentsImpl fileContentsDto =
FileContentsImpl.make().setMimeType(mimeType).setPath(currentPath);
if (editSession == null) {
// We need to start a new edit session.
String text = "";
File file = new File(currentPath);
try {
text = Files.toString(file, Charsets.UTF_8);
} catch (IOException e) {
logger.error(
String.format("Failed to read text contents for path [%s]", currentPath));
// Send back a no file indicating that file does not exist.
sendContent(message, currentPath, null, false);
return;
}
if (provisionEditSession) {
// Provision a new edit session and fall through.
editSession =
new FileEditSessionImpl(resourceId, currentPath, text, null, logger);
editSessions.put(resourceId, editSession);
// Update the last opened file.
vertx.eventBus().send("workspace.setLastOpenedFile",
new JsonObject().putString("resourceId", resourceId));
} else {
// Just send the contents as they were read from disk and return.
String dataBase64 = MimeTypes.looksLikeImage(mimeType) ? StringUtils
.newStringUtf8(Base64.encodeBase64(text.getBytes())) : null;
fileContentsDto.setContents(dataBase64).setContentType(
dataBase64 == null ? ContentType.UNKNOWN_BINARY : ContentType.IMAGE);
sendContent(message, currentPath, fileContentsDto, true);
return;
}
}
// Populate file contents response Dto with information from the edit session.
fileContentsDto.setFileEditSessionKey(resourceId)
.setCcRevision(editSession.getDocument().getCcRevision())
.setContents(editSession.getContents()).setContentType(ContentType.TEXT);
// Extract the contents from the edit session before sending.
sendContent(message, currentPath, fileContentsDto, true);