Skip to content

Commit 5e98000

Browse files
Updated Spring sample to correspond to common API
1 parent 98cb673 commit 5e98000

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

Demos/Spring/src/main/java/com/groupdocs/ui/viewer/spring/common/util/Utils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.nio.charset.StandardCharsets;
2121
import java.nio.file.Files;
2222
import java.nio.file.Path;
23+
import java.nio.file.Paths;
2324
import java.nio.file.StandardCopyOption;
2425
import java.util.List;
2526
import java.util.Map;
@@ -154,4 +155,13 @@ public static MediaType detectMediaType(String fileName) {
154155
}
155156
return MediaType.APPLICATION_OCTET_STREAM;
156157
}
158+
159+
public static String normalizePathToGuid(String filesDirectory, String path) {
160+
final Path relativePath = Paths.get(filesDirectory).relativize(Paths.get(path));
161+
return relativePath.toString().replace(File.separatorChar, '/');
162+
}
163+
164+
public static String normalizeGuidToPath(String path) {
165+
return path.replace('/', File.separatorChar);
166+
}
157167
}

Demos/Spring/src/main/java/com/groupdocs/ui/viewer/spring/service/ViewerServiceImpl.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ public void init() {
124124
*/
125125
@Override
126126
public List<FileDescriptionEntity> getFileList(String path) {
127-
final File filesDirectory = new File(PathUtils.combine(viewerConfiguration.getFilesDirectory(), path));
127+
final String filesDirectory = viewerConfiguration.getFilesDirectory();
128+
final File filesSubDirectory = new File(PathUtils.combine(filesDirectory, path));
128129

129130
List<FileDescriptionEntity> filesList = new ArrayList<>();
130131
try {
131-
final File[] files = filesDirectory.listFiles();
132+
final File[] files = filesSubDirectory.listFiles();
132133
if (files == null) {
133134
throw new TotalGroupDocsException("Can't list files");
134135
}
@@ -137,7 +138,7 @@ public List<FileDescriptionEntity> getFileList(String path) {
137138
// check if current file/folder is hidden
138139
if (!(file.getName().equals(viewerConfiguration.getCacheFolderName())) && !file.getName().startsWith(".") && !file.isHidden()) {
139140
FileDescriptionEntity fileDescription = new FileDescriptionEntity();
140-
fileDescription.setGuid(file.getCanonicalFile().getAbsolutePath());
141+
fileDescription.setGuid(Utils.normalizePathToGuid(filesDirectory, file.getCanonicalFile().getAbsolutePath()));
141142
fileDescription.setName(file.getName());
142143
// set is directory true/false
143144
fileDescription.setIsDirectory(file.isDirectory());
@@ -178,21 +179,22 @@ public int compare(FileDescriptionEntity o1, FileDescriptionEntity o2) {
178179
@Override
179180
public LoadDocumentEntity loadDocument(LoadDocumentRequest loadDocumentRequest, boolean loadAllPages, boolean printVersion) {
180181
// set request parameters
181-
String documentGuid = loadDocumentRequest.getGuid();
182+
final String documentGuid = loadDocumentRequest.getGuid();
183+
String documentPath = PathUtils.combine(viewerConfiguration.getFilesDirectory(), Utils.normalizeGuidToPath(documentGuid));
182184
String password = loadDocumentRequest.getPassword();
183185
password = org.apache.commons.lang3.StringUtils.isEmpty(password) ? null : password;
184186
LoadDocumentEntity loadDocumentEntity;
185187
CustomViewer<?> customViewer = null;
186188
try {
187-
final Path fileCachePath = createCacheDocumentDirectoryPath(documentGuid);
189+
final Path fileCachePath = createCacheDocumentDirectoryPath(documentPath);
188190
ViewerCache cache = new FileViewerCache(fileCachePath);
189191

190192
if (viewerConfiguration.isHtmlMode()) {
191-
customViewer = new HtmlViewer(documentGuid, cache, createLoadOptions(password));
193+
customViewer = new HtmlViewer(documentPath, cache, createLoadOptions(password));
192194
} else {
193-
customViewer = new PngViewer(documentGuid, cache, createLoadOptions(password));
195+
customViewer = new PngViewer(documentPath, cache, createLoadOptions(password));
194196
}
195-
loadDocumentEntity = getLoadDocumentEntity(loadAllPages, documentGuid, customViewer, printVersion);
197+
loadDocumentEntity = getLoadDocumentEntity(loadAllPages, documentPath, customViewer, printVersion);
196198
loadDocumentEntity.setShowGridLines(viewerConfiguration.getShowGridLines());
197199
loadDocumentEntity.setPrintAllowed(viewerConfiguration.getPrintAllowed());
198200
} catch (IncorrectPasswordException | PasswordRequiredException e) {
@@ -211,19 +213,20 @@ public LoadDocumentEntity loadDocument(LoadDocumentRequest loadDocumentRequest,
211213
*/
212214
@Override
213215
public PageDescriptionEntity loadDocumentPage(LoadDocumentPageRequest loadDocumentPageRequest) {
214-
String documentGuid = loadDocumentPageRequest.getGuid();
216+
final String documentGuid = loadDocumentPageRequest.getGuid();
217+
String documentPath = PathUtils.combine(viewerConfiguration.getFilesDirectory(), Utils.normalizeGuidToPath(documentGuid));
215218
Integer pageNumber = loadDocumentPageRequest.getPage();
216219
String password = loadDocumentPageRequest.getPassword();
217220
password = org.apache.commons.lang3.StringUtils.isEmpty(password) ? null : password;
218221
CustomViewer<?> customViewer = null;
219222
try {
220-
final Path cacheDocumentDirectoryPath = createCacheDocumentDirectoryPath(documentGuid);
223+
final Path cacheDocumentDirectoryPath = createCacheDocumentDirectoryPath(documentPath);
221224
ViewerCache cache = new FileViewerCache(cacheDocumentDirectoryPath);
222225

223226
if (viewerConfiguration.isHtmlMode()) {
224-
customViewer = new HtmlViewer(documentGuid, cache, createLoadOptions(password));
227+
customViewer = new HtmlViewer(documentPath, cache, createLoadOptions(password));
225228
} else {
226-
customViewer = new PngViewer(documentGuid, cache, createLoadOptions(password));
229+
customViewer = new PngViewer(documentPath, cache, createLoadOptions(password));
227230
}
228231
return getPageDescriptionEntity(customViewer, cacheDocumentDirectoryPath, pageNumber);
229232
} catch (Exception ex) {
@@ -242,15 +245,16 @@ public PageDescriptionEntity loadDocumentPage(LoadDocumentPageRequest loadDocume
242245
@Override
243246
public PageDescriptionEntity rotateDocumentPages(RotateDocumentPagesRequest rotateDocumentPagesRequest) {
244247
// set request parameters
245-
String documentGuid = rotateDocumentPagesRequest.getGuid();
248+
final String documentGuid = rotateDocumentPagesRequest.getGuid();
249+
String documentPath = PathUtils.combine(viewerConfiguration.getFilesDirectory(), Utils.normalizeGuidToPath(documentGuid));
246250
List<Integer> pages = rotateDocumentPagesRequest.getPages();
247251
String password = rotateDocumentPagesRequest.getPassword();
248252
Integer angle = rotateDocumentPagesRequest.getAngle();
249253
int pageNumber = pages.get(0);
250254
CustomViewer<?> customViewer = null;
251255

252256
try {
253-
final Path cacheDocumentDirectoryPath = createCacheDocumentDirectoryPath(documentGuid);
257+
final Path cacheDocumentDirectoryPath = createCacheDocumentDirectoryPath(documentPath);
254258

255259
// Delete cache files connected to the page
256260
for (File file : cacheDocumentDirectoryPath.toFile().listFiles(new FilenameFilter() {
@@ -272,9 +276,9 @@ public boolean accept(File dir, String name) {
272276
// Generate new cache
273277
ViewerCache cache = new FileViewerCache(cacheDocumentDirectoryPath);
274278
if (viewerConfiguration.isHtmlMode()) {
275-
customViewer = new HtmlViewer(documentGuid, cache, createLoadOptions(password), pageNumber, newAngle);
279+
customViewer = new HtmlViewer(documentPath, cache, createLoadOptions(password), pageNumber, newAngle);
276280
} else {
277-
customViewer = new PngViewer(documentGuid, cache, createLoadOptions(password), pageNumber, newAngle);
281+
customViewer = new PngViewer(documentPath, cache, createLoadOptions(password), pageNumber, newAngle);
278282
}
279283
return getPageDescriptionEntity(customViewer, cacheDocumentDirectoryPath, pageNumber);
280284

@@ -324,18 +328,19 @@ public ViewerConfiguration getViewerConfiguration() {
324328

325329
@Override
326330
public InputStream getPdf(LoadDocumentRequest loadDocumentRequest) {
327-
String documentGuid = loadDocumentRequest.getGuid();
331+
final String documentGuid = loadDocumentRequest.getGuid();
332+
String documentPath = PathUtils.combine(viewerConfiguration.getFilesDirectory(), Utils.normalizeGuidToPath(documentGuid));
328333
String password = loadDocumentRequest.getPassword();
329334
password = org.apache.commons.lang3.StringUtils.isEmpty(password) ? null : password;
330335
CustomViewer<?> customViewer = null;
331336
try {
332-
Path cacheDocumentDirectoryPath = createCacheDocumentDirectoryPath(documentGuid);
337+
Path cacheDocumentDirectoryPath = createCacheDocumentDirectoryPath(documentPath);
333338
ViewerCache cache = new FileViewerCache(cacheDocumentDirectoryPath);
334339

335340
if (viewerConfiguration.isHtmlMode()) {
336-
customViewer = new HtmlViewer(documentGuid, cache, createLoadOptions(password));
341+
customViewer = new HtmlViewer(documentPath, cache, createLoadOptions(password));
337342
} else {
338-
customViewer = new PngViewer(documentGuid, cache, createLoadOptions(password));
343+
customViewer = new PngViewer(documentPath, cache, createLoadOptions(password));
339344
}
340345
return getPdfFile(customViewer, cacheDocumentDirectoryPath);
341346
} catch (Exception ex) {
@@ -366,7 +371,7 @@ private PageDescriptionEntity getPageDescriptionEntity(CustomViewer<?> customVie
366371
return page;
367372
}
368373

369-
private LoadDocumentEntity getLoadDocumentEntity(boolean loadAllPages, String documentGuid, CustomViewer<?> customViewer, boolean printVersion) {
374+
private LoadDocumentEntity getLoadDocumentEntity(boolean loadAllPages, String documentPath, CustomViewer<?> customViewer, boolean printVersion) {
370375
try {
371376
if (loadAllPages) {
372377
customViewer.createCache();
@@ -378,7 +383,7 @@ private LoadDocumentEntity getLoadDocumentEntity(boolean loadAllPages, String do
378383
}
379384
LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity();
380385

381-
final Path cacheDocumentDirectoryPath = createCacheDocumentDirectoryPath(documentGuid);
386+
final Path cacheDocumentDirectoryPath = createCacheDocumentDirectoryPath(documentPath);
382387
PagesInfoStorage.createPagesInfo(cacheDocumentDirectoryPath, viewInfo, isViewerLicenseSet);
383388

384389
List<Page> pages = viewInfo.getPages();
@@ -395,11 +400,12 @@ private LoadDocumentEntity getLoadDocumentEntity(boolean loadAllPages, String do
395400
loadDocumentEntity.getPages().add(pageData);
396401
}
397402

398-
loadDocumentEntity.setGuid(documentGuid);
403+
final String filesDirectory = viewerConfiguration.getFilesDirectory();
404+
loadDocumentEntity.setGuid(Utils.normalizePathToGuid(filesDirectory, documentPath));
399405
return loadDocumentEntity;
400406
} catch (RuntimeException e) {
401407
if (e.getMessage() != null && e.getMessage().contains("At most 4 elements")) {
402-
throw new EvaluationModeException(documentGuid);
408+
throw new EvaluationModeException(documentPath);
403409
}
404410
logger.error("Something went wrong", e);
405411
throw new ReadWriteException(e);

0 commit comments

Comments
 (0)