Skip to content

Commit 128a1db

Browse files
committed
- Fixed getDocsUrl() to use WebdavService.getPath() instead of hardcoded /_webdav string
- Fixed hasDocumentation() to use Files.exists() instead of new File().exists() - getLocalPath() returns Path directly instead of converting to File - Updated getSupplementaryFileBasenames() to use Files.list() instead of File.list() - Refactored extractDocsFromZip() to use NIO Path - Use FileUtil.copyDirectory(Path, Path) to copy docs from previous tool version
1 parent 767b8bf commit 128a1db

2 files changed

Lines changed: 35 additions & 25 deletions

File tree

SkylineToolsStore/src/org/labkey/skylinetoolsstore/SkylineToolsStoreController.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
import java.net.URLDecoder;
101101
import java.nio.file.Files;
102102
import java.nio.file.Path;
103+
import java.nio.file.StandardCopyOption;
103104
import java.text.DateFormat;
104105
import java.text.SimpleDateFormat;
105106
import java.util.ArrayList;
@@ -230,11 +231,11 @@ protected byte[] unzip(ZipInputStream stream)
230231
}
231232
}
232233

233-
protected static boolean extractDocsFromZip(File zipFile, File containerDir) throws IOException
234+
protected static boolean extractDocsFromZip(Path zipFile, Path containerDir) throws IOException
234235
{
235-
File docsDir = new File(containerDir, "docs");
236+
Path docsDir = containerDir.resolve("docs");
236237
boolean extracted = false;
237-
try (ZipFile zf = new ZipFile(zipFile))
238+
try (ZipFile zf = new ZipFile(zipFile.toFile()))
238239
{
239240
Enumeration<? extends ZipEntry> entries = zf.entries();
240241
while (entries.hasMoreElements())
@@ -247,15 +248,14 @@ protected static boolean extractDocsFromZip(File zipFile, File containerDir) thr
247248
String relativePath = name.substring("tool-inf/docs/".length());
248249
if (relativePath.isEmpty())
249250
continue;
250-
File destFile = new File(docsDir, relativePath);
251+
Path destPath = docsDir.resolve(relativePath).normalize();
251252
// Zip-slip protection
252-
if (!destFile.getCanonicalPath().startsWith(docsDir.getCanonicalPath() + File.separator))
253+
if (!destPath.startsWith(docsDir.normalize()))
253254
throw new IOException("Zip entry outside target directory: " + name);
254-
Files.createDirectories(destFile.getParentFile().toPath());
255-
try (InputStream in = zf.getInputStream(entry);
256-
FileOutputStream out = new FileOutputStream(destFile))
255+
Files.createDirectories(destPath.getParent());
256+
try (InputStream in = zf.getInputStream(entry))
257257
{
258-
in.transferTo(out);
258+
Files.copy(in, destPath, StandardCopyOption.REPLACE_EXISTING);
259259
}
260260
extracted = true;
261261
}
@@ -265,12 +265,12 @@ protected static boolean extractDocsFromZip(File zipFile, File containerDir) thr
265265

266266
public static File makeFile(Container c, String filename)
267267
{
268-
return new File(getLocalPath(c), FileUtil.makeLegalName(filename));
268+
return getLocalPath(c).resolve(FileUtil.makeLegalName(filename)).toFile();
269269
}
270270

271-
public static File getLocalPath(Container c)
271+
public static Path getLocalPath(Container c)
272272
{
273-
return FileContentService.get().getFileRootPath(c, FileContentService.ContentType.files).toFile();
273+
return FileContentService.get().getFileRootPath(c, FileContentService.ContentType.files);
274274
}
275275

276276
protected Container makeContainer(Container parent, String folderName, List<User> users, Role role) throws IOException
@@ -417,7 +417,7 @@ public static ArrayList<String> getToolRelevantUsers(SkylineTool tool, Role[] ro
417417
return new ArrayList<>(users);
418418
}
419419

420-
public static HashMap<String, String> getSupplementaryFiles(SkylineTool tool)
420+
public static HashMap<String, String> getSupplementaryFiles(SkylineTool tool) throws IOException
421421
{
422422
// Store supporting files in map <url, icon url>
423423
final String[] knownExtensions = {"pdf", "zip"};
@@ -435,15 +435,15 @@ public static HashMap<String, String> getSupplementaryFiles(SkylineTool tool)
435435
return suppFiles;
436436
}
437437

438-
public static HashSet<String> getSupplementaryFileBasenames(SkylineTool tool)
438+
public static HashSet<String> getSupplementaryFileBasenames(SkylineTool tool) throws IOException
439439
{
440440
HashSet<String> suppFiles = new HashSet<>();
441-
File localToolDir = getLocalPath(tool.lookupContainer());
442-
for (String suppFile : localToolDir.list())
441+
Path localToolDir = getLocalPath(tool.lookupContainer());
442+
try (var stream = Files.list(localToolDir))
443443
{
444-
final String basename = new File(suppFile).getName();
445-
if (!basename.startsWith(".") && !basename.equals(tool.getZipName()) && !basename.equals("icon.png") && !basename.equals("docs"))
446-
suppFiles.add(suppFile);
444+
stream.map(p -> p.getFileName().toString())
445+
.filter(name -> !name.startsWith(".") && !name.equals(tool.getZipName()) && !name.equals("icon.png") && !name.equals("docs"))
446+
.forEach(suppFiles::add);
447447
}
448448
return suppFiles;
449449
}
@@ -622,12 +622,12 @@ else if (!getContainer().hasPermission(getUser(), InsertPermission.class))
622622
tool.writeIconToFile(makeFile(c, "icon.png"), "png");
623623

624624
// Extract docs from tool-inf/docs/ in the ZIP; carry forward from previous version if absent
625-
boolean hasDocs = extractDocsFromZip(storedZip, getLocalPath(c));
625+
boolean hasDocs = extractDocsFromZip(storedZip.toPath(), getLocalPath(c));
626626
if (!hasDocs && existingVersionContainer != null)
627627
{
628-
File oldDocs = new File(getLocalPath(existingVersionContainer), "docs");
629-
if (oldDocs.isDirectory())
630-
FileUtils.copyDirectory(oldDocs, new File(getLocalPath(c), "docs"));
628+
Path oldDocs = getLocalPath(existingVersionContainer).resolve("docs");
629+
if (Files.isDirectory(oldDocs))
630+
FileUtil.copyDirectory(oldDocs, getLocalPath(c).resolve("docs"));
631631
}
632632

633633
if (copyFiles != null && existingVersionContainer != null)

SkylineToolsStore/src/org/labkey/skylinetoolsstore/model/SkylineTool.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
import org.apache.commons.lang3.StringUtils;
44
import org.labkey.api.data.Container;
55
import org.labkey.api.data.Entity;
6+
import org.labkey.api.files.FileContentService;
67
import org.labkey.api.settings.AppProps;
78
import org.labkey.api.util.Pair;
9+
import org.labkey.api.webdav.WebdavService;
810
import org.labkey.skylinetoolsstore.SkylineToolsStoreController;
911

12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
1015
import javax.imageio.ImageIO;
1116
import java.io.BufferedReader;
1217
import java.io.ByteArrayInputStream;
@@ -295,12 +300,17 @@ public String getFolderUrl()
295300

296301
public boolean hasDocumentation()
297302
{
298-
return new File(SkylineToolsStoreController.getLocalPath(lookupContainer()), "docs/index.html").exists();
303+
Path localPath = SkylineToolsStoreController.getLocalPath(lookupContainer());
304+
return localPath != null && Files.exists(localPath.resolve("docs/index.html"));
299305
}
300306

301307
public String getDocsUrl()
302308
{
303-
return AppProps.getInstance().getContextPath() + "/_webdav" + lookupContainer().getPath() + "/@files/docs/index.html";
309+
org.labkey.api.util.Path path = WebdavService.getPath()
310+
.append(lookupContainer().getParsedPath())
311+
.append(FileContentService.FILES_LINK)
312+
.append(new org.labkey.api.util.Path("docs", "index.html"));
313+
return AppProps.getInstance().getContextPath() + path.encode();
304314
}
305315

306316
public String getIconUrl()

0 commit comments

Comments
 (0)