Skip to content

[WIKI-419] chore: new asset duplicate endpoint added #7172

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

Open
wants to merge 5 commits into
base: preview
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions apiserver/plane/app/urls/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ProjectAssetEndpoint,
ProjectBulkAssetEndpoint,
AssetCheckEndpoint,
DuplicateAssetEndpoint,
)


Expand Down Expand Up @@ -89,4 +90,9 @@
AssetCheckEndpoint.as_view(),
name="asset-check",
),
path(
"assets/v2/workspaces/<slug:slug>/duplicate-assets/",
DuplicateAssetEndpoint.as_view(),
name="duplicate-assets",
),
]
1 change: 1 addition & 0 deletions apiserver/plane/app/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
ProjectAssetEndpoint,
ProjectBulkAssetEndpoint,
AssetCheckEndpoint,
DuplicateAssetEndpoint,
)
from .issue.base import (
IssueListEndpoint,
Expand Down
90 changes: 90 additions & 0 deletions apiserver/plane/app/views/asset/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,3 +718,93 @@ def get(self, request, slug, asset_id):
id=asset_id, workspace__slug=slug, deleted_at__isnull=True
).exists()
return Response({"exists": asset}, status=status.HTTP_200_OK)


class DuplicateAssetEndpoint(BaseAPIView):
def get_entity_id_field(self, entity_type, entity_id):
# Workspace Logo
if entity_type == FileAsset.EntityTypeContext.WORKSPACE_LOGO:
return {"workspace_id": entity_id}

# Project Cover
if entity_type == FileAsset.EntityTypeContext.PROJECT_COVER:
return {"project_id": entity_id}

# User Avatar and Cover
if entity_type in [
FileAsset.EntityTypeContext.USER_AVATAR,
FileAsset.EntityTypeContext.USER_COVER,
]:
return {"user_id": entity_id}

# Issue Attachment and Description
if entity_type in [
FileAsset.EntityTypeContext.ISSUE_ATTACHMENT,
FileAsset.EntityTypeContext.ISSUE_DESCRIPTION,
]:
return {"issue_id": entity_id}

# Page Description
if entity_type == FileAsset.EntityTypeContext.PAGE_DESCRIPTION:
return {"page_id": entity_id}

# Comment Description
if entity_type == FileAsset.EntityTypeContext.COMMENT_DESCRIPTION:
return {"comment_id": entity_id}

return {}

@allow_permission([ROLE.ADMIN, ROLE.MEMBER], level="WORKSPACE")
def post(self, request, slug):
project_id = request.data.get("project_id", None)
asset_ids = request.data.get("asset_ids", None)
entity_id = request.data.get("entity_id", None)
entity_type = request.data.get("entity_type", None)

if not asset_ids:
return Response(
{"error": "asset_ids is required"}, status=status.HTTP_400_BAD_REQUEST
)

workspace = Workspace.objects.get(slug=slug)
if project_id:
# check if project exists in the workspace
if not Project.objects.filter(id=project_id, workspace=workspace).exists():
return Response(
{"error": "Project not found"}, status=status.HTTP_404_NOT_FOUND
)
duplicated_assets = {}

storage = S3Storage()
original_assets = FileAsset.objects.filter(
workspace=workspace, id__in=asset_ids
)
for original_asset in original_assets:
destination_key = f"{workspace.id}/{uuid.uuid4().hex}-{original_asset.attributes.get('name')}"
duplicated_asset = FileAsset.objects.create(
attributes={
"name": original_asset.attributes.get("name"),
"type": original_asset.attributes.get("type"),
"size": original_asset.attributes.get("size"),
},
asset=destination_key,
size=original_asset.size,
workspace=workspace,
created_by_id=request.user.id,
entity_type=entity_type,
project_id=project_id if project_id else None,
storage_metadata=original_asset.storage_metadata,
**self.get_entity_id_field(
entity_type=entity_type, entity_id=entity_id
),
)
storage.copy_object(original_asset.asset, destination_key)
duplicated_assets[str(original_asset.id)] = str(duplicated_asset.id)

if duplicated_assets:
# Update the is_uploaded field for all newly created assets
FileAsset.objects.filter(id__in=duplicated_assets.values()).update(
is_uploaded=True
)

return Response(duplicated_assets, status=status.HTTP_200_OK)
19 changes: 18 additions & 1 deletion web/core/services/file.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AxiosRequestConfig } from "axios";
// plane types
// plane imports
import { TFileEntityInfo, TFileSignedURLResponse } from "@plane/types";
import { EFileAssetType } from "@plane/types/src/enums";
// helpers
import { API_BASE_URL } from "@/helpers/common.helper";
import { generateFileUploadPayload, getAssetIdFromUrl, getFileMetaDataForUpload } from "@/helpers/file.helper";
Expand Down Expand Up @@ -258,6 +259,22 @@ export class FileService extends APIService {
});
}

async duplicateAssets(
workspaceSlug: string,
data: {
entity_id: string;
entity_type: EFileAssetType;
project_id?: string;
asset_ids: string[];
}
): Promise<Record<string, string>> {
return this.post(`/api/assets/v2/workspaces/${workspaceSlug}/duplicate-assets/`, data)
.then((response) => response?.data)
.catch((error) => {
throw error?.response?.data;
});
}

cancelUpload() {
this.cancelSource.cancel("Upload canceled");
}
Expand Down