Skip to content

Commit

Permalink
Clean up and simplify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
GspikeHalo committed Jan 17, 2025
1 parent c012f54 commit fe4dbf2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,36 +358,27 @@ class DatasetResource {
private def getDashboardDataset(
ctx: DSLContext,
did: UInteger,
uid: UInteger
uid: Option[UInteger],
isPublic: Boolean = false
): DashboardDataset = {
if (!userHasReadAccess(ctx, did, uid)) {
if (
(isPublic && !isDatasetPublic(ctx, did)) ||
(!isPublic && (!userHasReadAccess(ctx, did, uid.get)))
) {
throw new ForbiddenException(ERR_USER_HAS_NO_ACCESS_TO_DATASET_MESSAGE)
}

val targetDataset = getDatasetByID(ctx, did)
val userAccessPrivilege = getDatasetUserAccessPrivilege(ctx, did, uid)
val userAccessPrivilege =
if (isPublic) DatasetUserAccessPrivilege.NONE
else getDatasetUserAccessPrivilege(ctx, did, uid.get)
val isOwner = !isPublic && (targetDataset.getOwnerUid == uid.get)

DashboardDataset(
targetDataset,
getOwner(ctx, did).getEmail,
userAccessPrivilege,
targetDataset.getOwnerUid == uid,
List(),
calculateDatasetVersionSize(did)
)
}

def getPublicDashboardDataset(ctx: DSLContext, did: UInteger): DashboardDataset = {
if (!isDatasetPublic(ctx, did)) {
throw new ForbiddenException(ERR_USER_HAS_NO_ACCESS_TO_DATASET_MESSAGE)
}

val targetDataset = getDatasetByID(ctx, did)
DashboardDataset(
targetDataset,
getOwner(ctx, did).getEmail,
DatasetUserAccessPrivilege.NONE,
false,
isOwner,
List(),
calculateDatasetVersionSize(did)
)
Expand Down Expand Up @@ -714,17 +705,10 @@ class DatasetResource {
): List[DatasetVersion] = {
val uid = user.getUid
withTransaction(context)(ctx => {

if (!userHasReadAccess(ctx, did, uid)) {
throw new ForbiddenException(ERR_USER_HAS_NO_ACCESS_TO_DATASET_MESSAGE)
}
val result: java.util.List[DatasetVersion] = ctx
.selectFrom(DATASET_VERSION)
.where(DATASET_VERSION.DID.eq(did))
.orderBy(DATASET_VERSION.CREATION_TIME.desc()) // or .asc() for ascending
.fetchInto(classOf[DatasetVersion])

result.asScala.toList
fetchDatasetVersions(ctx, did)
})
}

Expand All @@ -734,24 +718,10 @@ class DatasetResource {
@PathParam("did") did: UInteger
): List[DatasetVersion] = {
withTransaction(context)(ctx => {

//
if (!isDatasetPublic(ctx, did)) {
throw new ForbiddenException(ERR_USER_HAS_NO_ACCESS_TO_DATASET_MESSAGE)
}

val result: java.util.List[DatasetVersion] = ctx
.selectFrom(DATASET_VERSION)
.where(DATASET_VERSION.DID.eq(did))
.orderBy(DATASET_VERSION.CREATION_TIME.desc()) // or .asc() for ascending
.fetchInto(classOf[DatasetVersion])

val datasetVersions = result.asScala.toList

// 打印返回结果
println(s"Returning DatasetVersions: $datasetVersions")

datasetVersions
fetchDatasetVersions(ctx, did)
})
}

Expand Down Expand Up @@ -812,37 +782,9 @@ class DatasetResource {
@Auth user: SessionUser
): DatasetVersionRootFileNodesResponse = {
val uid = user.getUid

withTransaction(context)(ctx => {
val dataset = getDashboardDataset(ctx, did, uid)
val targetDatasetPath = PathUtils.getDatasetPath(did)
val datasetVersion = getDatasetVersionByID(ctx, dvid)
val datasetName = dataset.dataset.getName
val fileNodes = GitVersionControlLocalFileStorage.retrieveRootFileNodesOfVersion(
targetDatasetPath,
datasetVersion.getVersionHash
)
val versionHash = getDatasetVersionByID(ctx, dvid).getVersionHash
val size = calculateDatasetVersionSize(did, Some(versionHash))
val ownerFileNode = DatasetFileNode
.fromPhysicalFileNodes(
Map((dataset.ownerEmail, datasetName, datasetVersion.getName) -> fileNodes.asScala.toList)
)
.head

DatasetVersionRootFileNodesResponse(
ownerFileNode.children.get
.find(_.getName == datasetName)
.head
.children
.get
.find(_.getName == datasetVersion.getName)
.head
.children
.get,
size
)
})
withTransaction(context)(ctx =>
fetchDatasetVersionRootFileNodes(ctx, did, dvid, Some(uid), isPublic = false)
)
}

@GET
Expand All @@ -851,36 +793,9 @@ class DatasetResource {
@PathParam("did") did: UInteger,
@PathParam("dvid") dvid: UInteger
): DatasetVersionRootFileNodesResponse = {
withTransaction(context)(ctx => {
val dataset = getPublicDashboardDataset(ctx, did)
val targetDatasetPath = PathUtils.getDatasetPath(did)
val datasetVersion = getDatasetVersionByID(ctx, dvid)
val datasetName = dataset.dataset.getName
val fileNodes = GitVersionControlLocalFileStorage.retrieveRootFileNodesOfVersion(
targetDatasetPath,
datasetVersion.getVersionHash
)
val versionHash = getDatasetVersionByID(ctx, dvid).getVersionHash
val size = calculateDatasetVersionSize(did, Some(versionHash))
val ownerFileNode = DatasetFileNode
.fromPhysicalFileNodes(
Map((dataset.ownerEmail, datasetName, datasetVersion.getName) -> fileNodes.asScala.toList)
)
.head

DatasetVersionRootFileNodesResponse(
ownerFileNode.children.get
.find(_.getName == datasetName)
.head
.children
.get
.find(_.getName == datasetVersion.getName)
.head
.children
.get,
size
)
})
withTransaction(context)(ctx =>
fetchDatasetVersionRootFileNodes(ctx, did, dvid, None, isPublic = true)
)
}

@GET
Expand All @@ -891,23 +806,15 @@ class DatasetResource {
@Auth user: SessionUser
): DashboardDataset = {
val uid = user.getUid
withTransaction(context)(ctx => {
val dashboardDataset = getDashboardDataset(ctx, did, uid)
val size = calculateDatasetVersionSize(did)
dashboardDataset.copy(size = size)
})
withTransaction(context)(ctx => fetchDataset(ctx, did, Some(uid), isPublic = false))
}

@GET
@Path("/public/{did}")
def getPublicDataset(
@PathParam("did") did: UInteger
): DashboardDataset = {
withTransaction(context)(ctx => {
val dashboardDataset = getPublicDashboardDataset(ctx, did)
val size = calculateDatasetVersionSize(did)
dashboardDataset.copy(size = size)
})
withTransaction(context)(ctx => fetchDataset(ctx, did, None, isPublic = true))
}

@GET
Expand Down Expand Up @@ -1052,4 +959,63 @@ class DatasetResource {

records.getValues(DATASET_USER_ACCESS.UID)
}

private def fetchDatasetVersions(ctx: DSLContext, did: UInteger): List[DatasetVersion] = {
ctx
.selectFrom(DATASET_VERSION)
.where(DATASET_VERSION.DID.eq(did))
.orderBy(DATASET_VERSION.CREATION_TIME.desc()) // Change to .asc() for ascending order
.fetchInto(classOf[DatasetVersion])
.asScala
.toList
}

private def fetchDatasetVersionRootFileNodes(
ctx: DSLContext,
did: UInteger,
dvid: UInteger,
uid: Option[UInteger],
isPublic: Boolean
): DatasetVersionRootFileNodesResponse = {
val dataset = getDashboardDataset(ctx, did, uid, isPublic)
val targetDatasetPath = PathUtils.getDatasetPath(did)
val datasetVersion = getDatasetVersionByID(ctx, dvid)
val datasetName = dataset.dataset.getName
val fileNodes = GitVersionControlLocalFileStorage.retrieveRootFileNodesOfVersion(
targetDatasetPath,
datasetVersion.getVersionHash
)
val versionHash = datasetVersion.getVersionHash
val size = calculateDatasetVersionSize(did, Some(versionHash))

val ownerFileNode = DatasetFileNode
.fromPhysicalFileNodes(
Map((dataset.ownerEmail, datasetName, datasetVersion.getName) -> fileNodes.asScala.toList)
)
.head

DatasetVersionRootFileNodesResponse(
ownerFileNode.children.get
.find(_.getName == datasetName)
.head
.children
.get
.find(_.getName == datasetVersion.getName)
.head
.children
.get,
size
)
}

private def fetchDataset(
ctx: DSLContext,
did: UInteger,
uid: Option[UInteger],
isPublic: Boolean
): DashboardDataset = {
val dashboardDataset = getDashboardDataset(ctx, did, uid, isPublic)
val size = calculateDatasetVersionSize(did)
dashboardDataset.copy(size = size)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h2 class="dataset-title">Dataset: {{datasetName}}</h2>
nzDescription="{{datasetDescription}}"></nz-card-meta>
<div
style="padding-top: 30px"
*ngIf="!disablePublish">
*ngIf="!(userDatasetAccessLevel === 'NONE')">
<nz-switch
[ngModel]="datasetIsPublic"
(ngModelChange)="onPublicStatusChange($event)"
Expand Down Expand Up @@ -44,7 +44,7 @@ <h3 class="file-title">
<button
nz-button
nz-tooltip="Download the file"
[disabled]="disableDownload"
[disabled]="!isLogin"
(click)="onClickDownloadCurrentFile()">
<i
nz-icon
Expand Down Expand Up @@ -164,7 +164,7 @@ <h6 style="font-weight: lighter; font-size: 0.9em">Choose a Version:</h6>
nz-tooltip="Download Dataset"
(click)="onClickDownloadVersionAsZip()"
*ngIf="selectedVersion"
[disabled]="disableDownload"
[disabled]="!isLogin"
class="spaced-button">
<i
nz-icon
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, NavigationEnd, Router } from "@angular/router";
import { ActivatedRoute, Router } from "@angular/router";
import { UntilDestroy, untilDestroyed } from "@ngneat/until-destroy";
import { DatasetService } from "../../../../service/user/dataset/dataset.service";
import { NzResizeEvent } from "ng-zorro-antd/resizable";
Expand Down Expand Up @@ -40,8 +40,6 @@ export class UserDatasetExplorerComponent implements OnInit {
public isCreatingDataset: boolean = false;
public versionCreatorBaseVersion: DatasetVersion | undefined;
public isLogin: boolean = this.userService.isLogin();
public disableDownload: boolean = false;
public disablePublish: boolean = false;

constructor(
private route: ActivatedRoute,
Expand Down Expand Up @@ -72,10 +70,6 @@ export class UserDatasetExplorerComponent implements OnInit {
}

ngOnInit(): void {
const fullPath = this.router.url;
if (fullPath.includes("hub")) {
this.applyHubRestrictions();
}
this.route.params
.pipe(
switchMap(params => {
Expand Down Expand Up @@ -119,13 +113,6 @@ export class UserDatasetExplorerComponent implements OnInit {
}
}

applyHubRestrictions() {
if (!this.isLogin) {
this.disableDownload = true;
}
this.disablePublish = true;
}

public onCreationFinished(creationID: number) {
if (creationID != 0) {
// creation succeed
Expand Down Expand Up @@ -241,7 +228,6 @@ export class UserDatasetExplorerComponent implements OnInit {
.retrieveDatasetVersionFileTree(this.did, this.selectedVersion.dvid, this.isLogin)
.pipe(untilDestroyed(this))
.subscribe(data => {
console.log(data);
this.fileTreeNodeList = data.fileNodes;
this.currentDatasetVersionSize = data.size;
let currentNode = this.fileTreeNodeList[0];
Expand Down

0 comments on commit fe4dbf2

Please sign in to comment.