-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added file download url endpoint (#44)
- Loading branch information
1 parent
807ba03
commit 2f5e543
Showing
6 changed files
with
199 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ namespace Player.Vm.Api.Domain.Models | |
{ | ||
public enum Permissions | ||
{ | ||
ReadOnly | ||
ReadOnly, | ||
ViewAdmin, | ||
SystemAdmin | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/Player.Vm.Api/Features/Vsphere/Commands/GetVmFileUrl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2024 Carnegie Mellon University. All Rights Reserved. | ||
// Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
using System.Runtime.Serialization; | ||
using System.Text.Json.Serialization; | ||
using Player.Vm.Api.Infrastructure.Exceptions; | ||
using Player.Vm.Api.Domain.Vsphere.Services; | ||
using Player.Vm.Api.Features.Vms; | ||
using System.IO; | ||
using AutoMapper; | ||
using Player.Vm.Api.Domain.Services; | ||
using System.Security.Principal; | ||
using Player.Vm.Api.Domain.Models; | ||
|
||
namespace Player.Vm.Api.Features.Vsphere | ||
{ | ||
public class GetVmFileUrl | ||
{ | ||
[DataContract(Name = "GetFileUrlVsphereVirtualMachine")] | ||
public class Command : IRequest<Response> | ||
{ | ||
[JsonIgnore] | ||
public Guid Id { get; set; } | ||
public string Username { get; set; } | ||
public string Password { get; set; } | ||
public string FilePath { get; set; } | ||
} | ||
|
||
[DataContract(Name = "FileVmUrlResponse")] | ||
public class Response | ||
{ | ||
public string Url { get; set; } | ||
public string FileName { get; set; } | ||
} | ||
|
||
public class Handler : BaseHandler, IRequestHandler<Command, Response> | ||
{ | ||
private readonly IVsphereService _vsphereService; | ||
|
||
public Handler( | ||
IVsphereService vsphereService, | ||
IVmService vmService, | ||
IMapper mapper, | ||
IPlayerService playerService, | ||
IPrincipal principal, | ||
IPermissionsService permissionsService) : | ||
base(mapper, vsphereService, playerService, principal, permissionsService, vmService) | ||
{ | ||
_vsphereService = vsphereService; | ||
} | ||
|
||
public async Task<Response> Handle(Command request, CancellationToken cancellationToken) | ||
{ | ||
var vm = await base.GetVm(request.Id, Permissions.ReadOnly, cancellationToken); | ||
|
||
if (!await _playerService.CanManageTeamsAsync(vm.TeamIds, false, cancellationToken)) | ||
throw new ForbiddenException("You do not have permission to download files from this vm."); | ||
|
||
var url = await _vsphereService.GetVmFileUrl( | ||
request.Id, | ||
request.Username, | ||
request.Password, | ||
request.FilePath); | ||
|
||
var fileName = Path.GetFileName(request.FilePath); | ||
|
||
return new Response() | ||
{ | ||
FileName = fileName, | ||
Url = url | ||
}; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters