resolveReferenceFilePath function

  1. @visibleForTesting
String resolveReferenceFilePath({
  1. required String filePath,
  2. required String referencePath,
})

Resolves filePath to an absolute path under referencePath.

Implementation

@visibleForTesting
String resolveReferenceFilePath({
  required String filePath,
  required String referencePath,
}) {
  final normalizedReference = p.normalize(p.absolute(referencePath));
  final resolved = p.isAbsolute(filePath)
      ? p.normalize(filePath)
      : p.normalize(p.join(normalizedReference, filePath));

  if (!p.isWithin(normalizedReference, resolved)) {
    throw PreviewException(
      'File must be under the reference directory ($normalizedReference).',
    );
  }

  return resolved;
}