Skip to content

Commit

Permalink
🚧 back: wip auto-create subdirectories as required when creating Driv…
Browse files Browse the repository at this point in the history
…eFile (#714)
  • Loading branch information
ericlinagora committed Nov 19, 2024
1 parent a2ba744 commit fd436ba
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions tdrive/backend/node/src/services/documents/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,66 @@ export class DocumentsService {
context: DriveExecutionContext,
): Promise<DriveFile> => {
try {
const driveItem = getDefaultDriveItem(content, context);
const driveItemVersion = getDefaultDriveItemVersion(version, context);
const driveItem = getDefaultDriveItem(content, context) as DriveFile; //TODO Why is this cast needed
const driveItemVersion = getDefaultDriveItemVersion(version, context) as FileVersion; //TODO Why is this cast needed
driveItem.scope = await getItemScope(driveItem, this.repository, context);

const createdFolderIds = [];
const pathComponents = driveItem.name.split("/"); //TODO Better path management
if (pathComponents[0] === "") pathComponents.shift(); // tolerate prefix /
if (
driveItem.is_directory &&
pathComponents.length &&
pathComponents[pathComponents.length - 1] === ""
)
pathComponents.pop(); // tolerate suffix / but only for directories
if (pathComponents.length === 0 || pathComponents.some(x => !x || x === "." || x === ".."))
throw new Error(`Invalid path: ${JSON.stringify(driveItem.name)}`);
if (pathComponents.length > 1) {
logger.debug({ path: driveItem.name, pathComponents }, "Creating intermediary folders"); //TODO NONONO
driveItem.name = pathComponents.pop();
if (!driveItem.name?.length)
throw new Error(`Invalid path ${JSON.stringify(driveItem.name)}: cannot end with a /`);
let lastParentId = driveItem.parent_id;
let couldNextExist = true;
for (const folderName of pathComponents) {
const existing =
couldNextExist &&
(await this.repository.findOne({ parent_id: lastParentId, name: folderName }));
if (existing) {
logger.debug({ folderName, id: existing.id }, " !!!! Already exists"); //TODO NONONO
if (existing.is_in_trash)
throw new Error(
`Error creating intermediary path ${JSON.stringify(
folderName,
)} under ${JSON.stringify(driveItem.parent_id)}: ${JSON.stringify(
folderName,
)} is in the trash`,
);
lastParentId = existing.id;
continue;
}
const newFolder = getDefaultDriveItem(
{
parent_id: lastParentId,
name: folderName,
is_directory: true,
},
context,
) as DriveFile; //TODO Why is this cast needed
newFolder.scope = driveItem.scope;
await this.repository.save(newFolder, context);
logger.info(
{ folderName, id: newFolder.id, parentId: lastParentId },
"Auto-created folder",
);
lastParentId = newFolder.id;
createdFolderIds.push(newFolder.id);
couldNextExist = false;
}
driveItem.parent_id = lastParentId;
}

const hasAccess = await checkAccess(
driveItem.parent_id,
null,
Expand Down

0 comments on commit fd436ba

Please sign in to comment.