Skip to content

Commit

Permalink
File download info for subfiles of artifacts/models/dataitems
Browse files Browse the repository at this point in the history
  • Loading branch information
kazhamiakin committed Aug 22, 2024
1 parent 54553a0 commit e2bbf6b
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,25 @@ public DownloadInfo downloadAsUrlArtifactById(
return filesService.downloadFileAsUrl(id);
}

@Operation(summary = "Get download url for a given artifact file, if available")
@GetMapping(path = "/{id}/files/download/{path:.*}", produces = "application/json; charset=UTF-8")
public DownloadInfo downloadAsUrlFile(
@PathVariable @Valid @NotNull @Pattern(regexp = Keys.SLUG_PATTERN) String project,
@PathVariable @Valid @NotNull @Pattern(regexp = Keys.SLUG_PATTERN) String id,
@PathVariable @Valid @NotNull @Pattern(regexp = Keys.FILE_PATTERN) String path
) throws NoSuchEntityException {
Artifact artifact = artifactService.getArtifact(id);

//check for project and name match
if (!artifact.getProject().equals(project)) {
throw new IllegalArgumentException("invalid project");
}

return filesService.downloadFileAsUrl(id, path);
}



@Operation(summary = "Create an upload url for a given artifact, if available")
@PostMapping(path = "/{id}/files/upload", produces = "application/json; charset=UTF-8")
public UploadInfo uploadAsUrlArtifactById(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,25 @@ public DownloadInfo downloadAsUrlById(
return filesService.downloadFileAsUrl(id);
}



@Operation(summary = "Get download url for a given artifact file, if available")
@GetMapping(path = "/{id}/files/download/{path:.*}", produces = "application/json; charset=UTF-8")
public DownloadInfo downloadAsUrlFile(
@PathVariable @Valid @NotNull @Pattern(regexp = Keys.SLUG_PATTERN) String project,
@PathVariable @Valid @NotNull @Pattern(regexp = Keys.SLUG_PATTERN) String id,
@PathVariable @Valid @NotNull @Pattern(regexp = Keys.FILE_PATTERN) String path
) throws NoSuchEntityException {
DataItem dataItem = dataItemService.getDataItem(id);

//check for project and name match
if (!dataItem.getProject().equals(project)) {
throw new IllegalArgumentException("invalid project");
}

return filesService.downloadFileAsUrl(id, path);
}

@Operation(summary = "Create an upload url for a given entity, if available")
@PostMapping(path = "/{id}/files/upload", produces = "application/json; charset=UTF-8")
public UploadInfo uploadAsUrlById(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ public DownloadInfo downloadAsUrlById(
return filesService.downloadFileAsUrl(id);
}

@Operation(summary = "Get download url for a given artifact file, if available")
@GetMapping(path = "/{id}/files/download/{path:.*}", produces = "application/json; charset=UTF-8")
public DownloadInfo downloadAsUrlFile(
@PathVariable @Valid @NotNull @Pattern(regexp = Keys.SLUG_PATTERN) String project,
@PathVariable @Valid @NotNull @Pattern(regexp = Keys.SLUG_PATTERN) String id,
@PathVariable @Valid @NotNull @Pattern(regexp = Keys.FILE_PATTERN) String path
) throws NoSuchEntityException {
Model model = modelService.getModel(id);

//check for project and name match
if (!model.getProject().equals(project)) {
throw new IllegalArgumentException("invalid project");
}

return filesService.downloadFileAsUrl(id, path);
}
@Operation(summary = "Create an upload url for a given entity, if available")
@PostMapping(path = "/{id}/files/upload", produces = "application/json; charset=UTF-8")
public UploadInfo uploadAsUrlById(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public interface ArtifactFilesService {

public DownloadInfo downloadFileAsUrl(@NotNull String id) throws NoSuchEntityException, SystemException;

public DownloadInfo downloadFileAsUrl(@NotNull String id, @NotNull String path) throws NoSuchEntityException, SystemException;

public UploadInfo uploadFileAsUrl(@Nullable String id, @NotNull String filename)
throws NoSuchEntityException, SystemException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public interface DataItemFilesService {

public DownloadInfo downloadFileAsUrl(@NotNull String id) throws NoSuchEntityException, SystemException;

public DownloadInfo downloadFileAsUrl(@NotNull String id, @NotNull String path) throws NoSuchEntityException, SystemException;

public UploadInfo uploadFileAsUrl(@Nullable String id, @NotNull String filename)
throws NoSuchEntityException, SystemException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public interface ModelFilesService {

public DownloadInfo downloadFileAsUrl(@NotNull String id) throws NoSuchEntityException, SystemException;

public DownloadInfo downloadFileAsUrl(@NotNull String id, @NotNull String path) throws NoSuchEntityException, SystemException;

public UploadInfo uploadFileAsUrl(@Nullable String id, @NotNull String filename)
throws NoSuchEntityException, SystemException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,37 @@ public DownloadInfo downloadFileAsUrl(@NotNull String id) throws NoSuchEntityExc
}
}

@Override
public DownloadInfo downloadFileAsUrl(@NotNull String id, @NotNull String sub) throws NoSuchEntityException, SystemException {
log.debug("download url for artifact file with id {} and path {}", String.valueOf(id), String.valueOf(sub));

try {
Artifact artifact = entityService.get(id);

//extract path from spec
ArtifactBaseSpec spec = new ArtifactBaseSpec();
spec.configure(artifact.getSpec());

String path = spec.getPath();
if (!StringUtils.hasText(path)) {
throw new NoSuchEntityException("file");
}
if (!path.endsWith("/")) path += "/";
path += sub;

DownloadInfo info = filesService.getDownloadAsUrl(path);
if (log.isTraceEnabled()) {
log.trace("download url for artifact with id {} and path {}: {} -> {}", id, sub, path, info);
}

return info;
} catch (NoSuchEntityException e) {
throw new NoSuchEntityException(EntityName.ARTIFACT.toString());
} catch (StoreException e) {
log.error("store error: {}", e.getMessage());
throw new SystemException(e.getMessage());
}
}
@Override
public List<FileInfo> getFileInfo(@NotNull String id) throws NoSuchEntityException, SystemException {
log.debug("get files info for artifact with id {}", String.valueOf(id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,37 @@ public DownloadInfo downloadFileAsUrl(@NotNull String id) throws NoSuchEntityExc
}
}

@Override
public DownloadInfo downloadFileAsUrl(@NotNull String id, @NotNull String sub) throws NoSuchEntityException, SystemException {
log.debug("download url for dataitem file with id {} and path {}", String.valueOf(id), String.valueOf(sub));

try {
DataItem dataItem = entityService.get(id);

//extract path from spec
DataItemBaseSpec spec = new DataItemBaseSpec();
spec.configure(dataItem.getSpec());

String path = spec.getPath();
if (!StringUtils.hasText(path)) {
throw new NoSuchEntityException("file");
}
if (!path.endsWith("/")) path += "/";
path += sub;

DownloadInfo info = filesService.getDownloadAsUrl(path);
if (log.isTraceEnabled()) {
log.trace("download url for dataitem with id {} and path {}: {} -> {}", id, sub, path, info);
}

return info;
} catch (NoSuchEntityException e) {
throw new NoSuchEntityException(EntityName.DATAITEM.toString());
} catch (StoreException e) {
log.error("store error: {}", e.getMessage());
throw new SystemException(e.getMessage());
}
}
@Override
public List<FileInfo> getFileInfo(@NotNull String id) throws NoSuchEntityException, SystemException {
log.debug("get files info for entity with id {}", String.valueOf(id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,38 @@ public DownloadInfo downloadFileAsUrl(@NotNull String id) throws NoSuchEntityExc
}
}

@Override
public DownloadInfo downloadFileAsUrl(@NotNull String id, @NotNull String sub) throws NoSuchEntityException, SystemException {
log.debug("download url for model file with id {} and path {}", String.valueOf(id), String.valueOf(sub));

try {
Model model = entityService.get(id);

//extract path from spec
ModelBaseSpec spec = new ModelBaseSpec();
spec.configure(model.getSpec());

String path = spec.getPath();
if (!StringUtils.hasText(path)) {
throw new NoSuchEntityException("file");
}
if (!path.endsWith("/")) path += "/";
path += sub;

DownloadInfo info = filesService.getDownloadAsUrl(path);
if (log.isTraceEnabled()) {
log.trace("download url for model with id {} and path {}: {} -> {}", id, sub, path, info);
}

return info;
} catch (NoSuchEntityException e) {
throw new NoSuchEntityException(EntityName.MODEL.toString());
} catch (StoreException e) {
log.error("store error: {}", e.getMessage());
throw new SystemException(e.getMessage());
}
}

@Override
public List<FileInfo> getFileInfo(@NotNull String id) throws NoSuchEntityException, SystemException {
log.debug("get storage metadata for model with id {}", String.valueOf(id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Keys {
public static final String STORE_PREFIX = "store://";
public static final String PATH_DIVIDER = "/";
public static final String ID_DIVIDER = ":";

public static final String FILE_PATTERN = "(([^/]+)/)*([^/]+)";

private Keys() {}
}

0 comments on commit e2bbf6b

Please sign in to comment.