enonic / lib-static

Library for serving static assets through XP, optimized for client caching
Apache License 2.0
0 stars 0 forks source link

Implement IoService isDirectory #382

Closed ComLock closed 1 week ago

ComLock commented 1 week ago

https://github.com/enonic/lib-static/blob/9720787ba73057d7ae89377f962895e0566d57d2/src/main/java/lib/enonic/libStatic/IoService.java#L146

According to @rymsha we can assume that is a resource has size 0 it's a directory.

However when I run some tests, it seems Enonic doesn't know the difference between a non-existant folder and an actual one.

import { getResource } from '/lib/xp/io';

[
  '/non-existant',
  '/non-existant/',
  '/static/folder',
  '/static/folder/',
  '/static/folder/index.html',
  '/static/folder/index.html/',
].forEach((path) => {
  const resource = getResource(path)
  log.info('path:%s exists:%s', path, resource.exists());
  log.info('path:%s size:%s', path, resource.getSize());
  log.info('path:%s timestamp:%s', path, resource.getTimestamp());
});
2024-09-10 14:10:28,843 INFO  com.example.typescript - (/main.js) path:/non-existant exists:false
2024-09-10 14:10:28,844 INFO  com.example.typescript - (/main.js) path:/non-existant size:-1
2024-09-10 14:10:28,845 INFO  com.example.typescript - (/main.js) path:/non-existant timestamp:-1
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/non-existant/ exists:false
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/non-existant/ size:-1
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/non-existant/ timestamp:-1
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/static/folder exists:false
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/static/folder size:-1
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/static/folder timestamp:-1
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/static/folder/ exists:false
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/static/folder/ size:-1
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/static/folder/ timestamp:-1
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/static/folder/index.html exists:true
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/static/folder/index.html size:497
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/static/folder/index.html timestamp:1725963024000
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/static/folder/index.html/ exists:true
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/static/folder/index.html/ size:497
2024-09-10 14:10:28,846 INFO  com.example.typescript - (/main.js) path:/static/folder/index.html/ timestamp:1725963024000
ComLock commented 1 week ago

Maybe

final URL url = resource.getUrl();
          if ( url == null )
          {
              return false;
          }
          try
          {
              if ( Files.isDirectory( Path.of( url.toURI() ) ) )
              {
                  return true;
              }
              return false;
          }
          catch ( URISyntaxException e )
          {
              return false;
          }
rymsha commented 1 week ago

isDirectory must be carefully implemented. Existing methods of IOService can't tell the difference between 0 size file and non-existent file