Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fixed bug with downloading zip folder #541

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ export class CrudException extends Error {
return new CrudException(details, 400);
}

static unauthorized(details: string): CrudException {
return new CrudException(details, 401);
}

static notFound(details: string): CrudException {
return new CrudException(details, 404);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import config from "../../../../config";
import { JwtType } from "../../types";
import { executionStorage } from "../../../framework/execution-storage";
import gr from "../../../../../services/global-resolver";
import { CrudException } from "../../../framework/api/crud-service";

const jwtPlugin: FastifyPluginCallback = async (fastify, _opts, next) => {
fastify.register(cookie);
Expand Down Expand Up @@ -50,7 +51,7 @@ const jwtPlugin: FastifyPluginCallback = async (fastify, _opts, next) => {
try {
await authenticate(request);
} catch (err) {
throw fastify.httpErrors.unauthorized(`Bad credentials ${JSON.stringify(err)}`);
throw CrudException.unauthorized(`Bad credentials: ${err.message}`);
}
});

Expand Down
5 changes: 3 additions & 2 deletions tdrive/backend/node/src/services/documents/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,11 @@ export const addDriveItemToArchive = async (
context: CompanyExecutionContext,
prefix?: string,
): Promise<void> => {
const item = entity || (await repository.findOne({ id, company_id: context.company.id }));
const itemPK = { id, company_id: context.company.id };
const item = entity || (await repository.findOne(itemPK));

if (!item) {
throw Error("item not found");
throw Error(`Item '${JSON.stringify(itemPK)}' not found`);
}

if (!item.is_directory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ export class DocumentsController {
});

archive.pipe(reply.raw);

return reply;
} catch (error) {
logger.error({ error: `${error}` }, "failed to send zip file");
throw new CrudException("Failed to create zip file", 500);
Expand Down
10 changes: 1 addition & 9 deletions tdrive/backend/node/test/e2e/common/user-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ export default class UserApi {
});
};

async getFolder(id: string | "root" | "trash" | "shared_with_me") {
async zipDocument(id: string | "root" | "trash" | "shared_with_me") {
return await this.platform.app.inject({
method: "GET",
url: `${UserApi.DOC_URL}/companies/${this.platform.workspace.company_id}/item/download/zip?items=${id}`,
Expand All @@ -410,21 +410,13 @@ export default class UserApi {
});
};


async getDocumentOKCheck(id: string | "root" | "trash" | "shared_with_me") {
const response = await this.getDocument(id);
expect(response.statusCode).toBe(200);
const doc = deserialize<DriveItemDetailsMockClass>(DriveItemDetailsMockClass, response.body);
expect(doc.item?.id).toBe(id);
};

async getFolderOKCheck(id: string | "root" | "trash" | "shared_with_me") {
const response = await this.getFolder(id);
expect(response.statusCode).toBe(200);
const doc = deserialize<DriveItemDetailsMockClass>(DriveItemDetailsMockClass, response.body);
expect(doc.item?.id).toBe(id);
};

async sharedWithMeDocuments(
payload: Record<string, any>
) {
Expand Down
16 changes: 16 additions & 0 deletions tdrive/backend/node/test/e2e/documents/documents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ describe("the Drive feature", () => {
expect(updateItemResult.name).toEqual("somethingelse");
});

it("Download folder as a zip should work fine", async () => {
//given
const folder = await currentUser.createDirectory("user_" + currentUser.user.id)
await currentUser.uploadRandomFileAndCreateDocument(folder.id);

//when
const zipResponse = await currentUser.zipDocument(folder.id);

//then
expect(zipResponse).toBeTruthy();
expect(zipResponse.statusCode).toBe(200);

//and data is in place
expect(zipResponse.body.length).toBeGreaterThanOrEqual(100);
});

it("did move an item to trash", async () => {
const createItemResult = await currentUser.createDefaultDocument();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ describe("the public links feature", () => {
anotherUser.jwt = token.value;
await anotherUser.getDocumentOKCheck(doc.id);

expect((await anotherUser.getFolder(doc.id)).statusCode).toBe(200);
expect((await anotherUser.zipDocument(doc.id)).statusCode).toBe(200);
});
});
});
Loading