Skip to content

Commit

Permalink
Updating listing to incorporate .acp files
Browse files Browse the repository at this point in the history
  • Loading branch information
Dexagod committed Jan 7, 2025
1 parent c3e7e6b commit ef6a895
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/commands/solid-copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ async function getRemoteSourceFiles(source: SourceOptions, fetch: typeof globalT
function readLocalFile(path: string, verbose: boolean, options?: { logger?: Logger }): { buffer: Buffer, contentType: string} {
if (verbose) (options?.logger || console).log('Reading local file:', path)
const file = fs.readFileSync(path)
let contentType = path.endsWith('.acl') || path.endsWith('.meta') ? 'text/turtle' : mime.lookup(path)
let contentType = path.endsWith('.acl') || path.endsWith('.meta') ? 'text/turtle': path.endsWith('.acp') ? 'application/ld+json': mime.lookup(path)
return { buffer: file, contentType };
}

Expand Down Expand Up @@ -279,6 +279,7 @@ async function writeLocalFile(resourcePath: string, fileInfo: FileInfo, options:
let ext = path.extname(resourcePath)
// Hardcode missing common extensions
if (resourcePath.endsWith('.acl')) ext = '.acl'
if (resourcePath.endsWith('.acp')) ext = '.acp'
if (resourcePath.endsWith('.meta')) ext = '.meta'
if (!ext) {
const extension = mime.extension(fileData.contentType)
Expand Down Expand Up @@ -379,6 +380,8 @@ function readLocalDirectoryRecursively(
directories.push({ absolutePath: resourcePath + resource + '/', relativePath: local_path + resource + '/'});
} else if (resource.endsWith('.acl')) {
if (options.all) { aclfiles.push({ absolutePath: resourcePath + resource, relativePath: local_path + resource }) }
} else if (resource.endsWith('.acp')) {
if (options.all) { aclfiles.push({ absolutePath: resourcePath + resource, relativePath: local_path + resource }) }
} else {
files.push({ absolutePath: resourcePath + resource, relativePath: local_path + resource });
}
Expand Down
10 changes: 7 additions & 3 deletions src/commands/solid-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@ export default async function list(url: string, options?: ICommandOptionsList) {
if (commandOptions.all) {
let headerInfo = await checkHeadersForAclAndMetadata(url, commandOptions.fetch)
if (headerInfo.acl) {
let resourceInfo = await getResourceInfoFromHeaders(headerInfo.acl, url, commandOptions.fetch)
const resourceInfo = await getResourceInfoFromHeaders(headerInfo.acl, url, commandOptions.fetch)
if(resourceInfo) resourceInfos.push(resourceInfo)
}
if (headerInfo.meta) {
let resourceInfo = await getResourceInfoFromHeaders(headerInfo.meta, url, commandOptions.fetch)
const resourceInfo = await getResourceInfoFromHeaders(headerInfo.meta, url, commandOptions.fetch)
if(resourceInfo) resourceInfos.push(resourceInfo)
}
}

const promiseList: Promise<ResourceInfo>[] = [];
for (let containedResourceUrl of containedResources) {
promiseList.push(new Promise((resolve, reject) => {
let resourceInfo = getResourceInfoFromDataset(dataset, containedResourceUrl, url);
if (resourceInfo && !resourceInfo.isDir && commandOptions.all) { // We only want to show acl files in the current dir. Aka the ones of the current dir + the ones of contained files
getAclAndMetadata(containedResourceUrl, url, commandOptions.fetch)
.then((headerResources) => {
console.log('headerResources', headerResources)
if (headerResources.acl) resourceInfo.acl = headerResources.acl
if (headerResources.meta) resourceInfo.metadata = headerResources.meta
resolve(resourceInfo);
Expand All @@ -59,12 +61,14 @@ export default async function list(url: string, options?: ICommandOptionsList) {
}))
}


const metadataResourceInfoList = (await Promise.all(promiseList)).filter((e) => !!e)
console.log(JSON.stringify(metadataResourceInfoList, null, 2))
resourceInfos = resourceInfos.concat(metadataResourceInfoList)
metadataResourceInfoList.forEach( (resourceInfo: ResourceInfo) => {
if(resourceInfo.acl) resourceInfos.push(resourceInfo.acl)
if(resourceInfo.metadata) resourceInfos.push(resourceInfo.metadata)
});
return resourceInfos
return resourceInfos.filter(resource => resource.url.startsWith(url)) // ugly workaround to .acl and .acp resources not appearing in container

}
2 changes: 1 addition & 1 deletion src/commands/solid-touch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default async function touch(url: string, options?: ICommandOptionsTouch)
else {
let path = url.replace(/.*\//,'')
let mimetype = mime.lookup(path)
let contentType = (path.endsWith('.acl') || path.endsWith('.meta') || !mimetype) ? 'text/turtle' : mimetype
let contentType = path.endsWith('.acl') || path.endsWith('.meta') ? 'text/turtle': path.endsWith('.acp') ? 'application/ld+json': mime.lookup(path)

if (!contentType) {
throw new Error('Could not discover content type for the touched resource. Please add a file extension to the touched resource.')
Expand Down
8 changes: 5 additions & 3 deletions src/shell/commands/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ function formatListing(listings: any[], options: any) {
let values = listings.map((listingInfo) => {
let path = options.full
? listingInfo.url
: listingInfo.relativePath
: getResourceInfoRelativePath(listingInfo)

if (listingInfo.isDir) return chalk.blue.bold(path)
else if (path.endsWith('.acl')) return chalk.red(path)
else if (path.endsWith('.acp')) return chalk.red(path)
else if (path.endsWith('.meta')) return chalk.greenBright(path)
else return path
})
Expand All @@ -77,10 +78,10 @@ function formatListing(listings: any[], options: any) {
const fileNameLengths = listings.map(fileInfo => options.full ? fileInfo.url.length : getResourceInfoRelativePath(fileInfo).length)
const fileNameFieldLength = Math.max(...[Math.max(...fileNameLengths.map(x => x || 0)), 8])

const aclLengths = listings.map(fileInfo => fileInfo.acl ? (options.full ? fileInfo.acl.url.length : fileInfo.acl.relativePath.length) : 0)
const aclLengths = listings.map(fileInfo => fileInfo.acl ? (options.full ? fileInfo.acl.url.length : getResourceInfoRelativePath(fileInfo.acl).length) : 0)
const aclFieldLength = Math.max(...[Math.max(...aclLengths.map(x => x || 0)), 3])

const metaLengths = listings.map(fileInfo => fileInfo.metadata ? (options.full ? fileInfo.metadata.url.length : fileInfo.metadata.relativePath.length) : 0)
const metaLengths = listings.map(fileInfo => fileInfo.metadata ? (options.full ? fileInfo.metadata.url.length : getResourceInfoRelativePath(fileInfo.metadata).length) : 0)
const metaFieldLength = Math.max(...[Math.max(...metaLengths.map(x => x || 0)), 4])

const mtimeLength = listings.map(listingInfo => listingInfo.mtime ? listingInfo.mtime.toString().length : 0)
Expand Down Expand Up @@ -111,6 +112,7 @@ function formatListing(listings: any[], options: any) {
let pathString = '';
if (listingInfo.isDir) pathString = chalk.blue.bold(path.padEnd(fileNameFieldLength))
else if (path.endsWith('.acl')) pathString = chalk.red(path.padEnd(fileNameFieldLength))
else if (path.endsWith('.acp')) pathString = chalk.red(path.padEnd(fileNameFieldLength))
else if (path.endsWith('.meta')) pathString = chalk.greenBright(path.padEnd(fileNameFieldLength))
else pathString = path.padEnd(fileNameFieldLength)

Expand Down
2 changes: 1 addition & 1 deletion src/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ export async function getResourceInfoFromHeaders(resourceUrl: string, containerU
const types = linkTypes.length ? linkTypes : undefined
const resourceInfo : ResourceInfo = {
url: resourceUrl,
relativePath: containerUrl ? resourceUrl.slice(containerUrl.length) : undefined,
relativePath: containerUrl && resourceUrl.startsWith(containerUrl) ? resourceUrl.slice(containerUrl.length) : undefined,
isDir: isDirectory(resourceUrl),
modified, types
}
Expand Down

0 comments on commit ef6a895

Please sign in to comment.