Skip to content

Commit

Permalink
porting aspnet demo, partially working
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprodan committed Aug 2, 2016
1 parent 7b041fe commit 1846feb
Show file tree
Hide file tree
Showing 585 changed files with 120,632 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ nuget.exe
project.lock.json

bower_components/
node_modules/
**/wwwroot/lib
node_modules/
9 changes: 9 additions & 0 deletions SwiftClient.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SwiftClient.Test", "test\Sw
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{D2397301-E397-40ED-8307-A9D94C7E687A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{B791F94A-97EF-45EE-8E67-A43704F459B5}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SwiftClient.AspNetCore", "samples\SwiftClient.AspNetCore\SwiftClient.AspNetCore.xproj", "{B1D7D776-743F-4819-87F1-5DDFD3D26C07}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -30,12 +34,17 @@ Global
{5985B113-1A1C-4EFD-B06C-341C4B9467E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5985B113-1A1C-4EFD-B06C-341C4B9467E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5985B113-1A1C-4EFD-B06C-341C4B9467E9}.Release|Any CPU.Build.0 = Release|Any CPU
{B1D7D776-743F-4819-87F1-5DDFD3D26C07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B1D7D776-743F-4819-87F1-5DDFD3D26C07}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B1D7D776-743F-4819-87F1-5DDFD3D26C07}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B1D7D776-743F-4819-87F1-5DDFD3D26C07}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{C855AA03-FD6C-422F-99FA-4A0BFCB67F39} = {2260B1B4-9A09-425D-8A62-511C8AA95C78}
{5985B113-1A1C-4EFD-B06C-341C4B9467E9} = {D2397301-E397-40ED-8307-A9D94C7E687A}
{B1D7D776-743F-4819-87F1-5DDFD3D26C07} = {B791F94A-97EF-45EE-8E67-A43704F459B5}
EndGlobalSection
EndGlobal
3 changes: 3 additions & 0 deletions samples/SwiftClient.AspNetCore/.bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"directory": "wwwroot/lib"
}
27 changes: 27 additions & 0 deletions samples/SwiftClient.AspNetCore/Controllers/ErrorController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Mvc;

namespace SwiftClient.AspNetCore.Controllers
{
public class ErrorController : Controller
{
public ErrorController()
{

}

public IActionResult Http403()
{
return View("Forbidden");
}

public IActionResult Http404()
{
return View("NotFound");
}

public IActionResult Http500()
{
return View("ServerError");
}
}
}
266 changes: 266 additions & 0 deletions samples/SwiftClient.AspNetCore/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System.IO;
using Microsoft.Extensions.Options;

namespace SwiftClient.AspNetCore.Controllers
{
public class HomeController : Controller
{
string containerTempId = "demotempcontainer";
string containerDemoId = "democontainer";

string metaFileName = "Filename";
string metaContentType = "Contenttype";

SwiftCredentials Credentials;
Client Client;

public HomeController(IOptions<SwiftCredentials> credentials, IMemoryCache cache)
{
Credentials = credentials.Value;

Client = new Client(new SwiftAuthManagerWithCache(Credentials, cache));

Client.SetRetryCount(2)
.SetLogger(new SwiftLogger());
}

public async Task<IActionResult> Index()
{
var viewModel = new PageViewModel();

var authData = await Client.Authenticate();

if (authData != null)
{
viewModel.Message = $"Connected on proxy node: {authData.StorageUrl} with authentication token: {authData.AuthToken}";

viewModel.Tree = await GetTree();
}
else
{
viewModel.Message = $"Error connecting to proxy node: {Credentials.Endpoints.First()} with credentials: {Credentials.Username} / {Credentials.Password}";
}

return View(viewModel);
}

public async Task<IActionResult> UploadChunk(int segment)
{
if (Request.Form.Files != null && Request.Form.Files.Count > 0)
{
var file = Request.Form.Files[0];
var fileName = file.GetFileName();

using (var fileStream = file.OpenReadStream())
{
using (var memoryStream = new MemoryStream())
{
await fileStream.CopyToAsync(memoryStream);

var resp = await Client.PutObjectChunk(containerTempId, fileName, memoryStream.ToArray(), segment);

return new JsonResult(new
{
ContentType = file.ContentType,
FileName = fileName ?? "demofile",
Status = resp.StatusCode,
Message = resp.Reason,
Success = resp.IsSuccess
});
}
}
}

return new JsonResult(new
{
Success = false
});
}

public async Task<IActionResult> UploadDone(int segmentsCount, string fileName, string contentType)
{
// use manifest to merge chunks
await Client.PutManifest(containerTempId, fileName);

// copy chunks to new file and set some meta data info about the file (filename, contentype)
await Client.CopyObject(containerTempId, fileName, containerDemoId, fileName, new Dictionary<string, string>
{
{ $"X-Object-Meta-{metaFileName}", fileName },
{ $"X-Object-Meta-{metaContentType}", contentType }
});

// cleanup temp
await Client.DeleteContainerContents(containerTempId);

return new JsonResult(new
{
Success = true
});
}

public async Task<IActionResult> PlayVideo(string containerId, string objectId)
{
var headObject = await Client.HeadObject(containerId, objectId);

if (headObject.IsSuccess)
{
var fileName = headObject.GetMeta(metaFileName);
var contentType = headObject.GetMeta(metaContentType);

var stream = new BufferedHTTPStream((start, end) =>
{
using (var response = Client.GetObjectRange(containerId, objectId, start, end).Result)
{
var ms = new MemoryStream();
response.Stream.CopyTo(ms);
return ms;
}
}, () => headObject.ContentLength);

Response.Headers.Add("Content-Disposition", $"attachment; filename={fileName}");

return new VideoStreamResult(stream, "video/mp4");
}

return new NotFoundResult();
}

public async Task<IActionResult> DownloadFile(string containerId, string objectId)
{
var headObject = await Client.HeadObject(containerId, objectId);

if (headObject.IsSuccess && headObject.ContentLength > 0)
{
var fileName = headObject.GetMeta(metaFileName);
var contentType = headObject.GetMeta(metaContentType);

Response.Headers.Add("Content-Disposition", $"attachment; filename={fileName}");

var stream = new BufferedHTTPStream((start, end) =>
{
using (var response = Client.GetObjectRange(containerId, objectId, start, end).Result)
{
var ms = new MemoryStream();
response.Stream.CopyTo(ms);
return ms;
}
}, () => headObject.ContentLength);

return new FileStreamResult(stream, contentType ?? "application/octet-stream");
}

return new NotFoundResult();
}

public async Task<IActionResult> RefreshTree()
{
return new JsonResult(new
{
Data = await GetTree()
});
}

private async Task<TreeViewModel> GetTree()
{
var tree = new TreeViewModel();

var accountData = await Client.GetAccount();

if (accountData.IsSuccess)
{
tree.Text = Credentials.Username;

if (accountData.Containers != null)
{
tree.Nodes = new List<TreeViewModel>();

var tasks = new List<Task<TreeViewModel>>();

foreach (var container in accountData.Containers)
{
tasks.Add(GetContainerBranch(container.Container));
}

await Task.WhenAll(tasks).ContinueWith((rsp) =>
{
tree.Nodes.AddRange(rsp.Result);
});
}
}

return tree;
}

private async Task<TreeViewModel> GetContainerBranch(string containerId)
{
var containerData = await Client.GetContainer(containerId);

TreeViewModel result = new TreeViewModel
{
Text = containerId,
ContainerId = containerId
};

if (containerData.IsSuccess)
{
if (containerData.Objects != null && containerData.ObjectsCount > 0)
{
result.Nodes = GetObjectBranch(containerId, "", containerData.Objects.Select(x => x.Object).ToList()).ToList();

if (result.Nodes != null && result.Nodes.Any())
{
result.HasNodes = true;
}
}
}

return result;
}

private List<TreeViewModel> GetObjectBranch(string containerId, string prefixObj, List<string> objectIds)
{
var prefixes = objectIds.Select(x => x.Split('\\')[0]).Distinct().ToList();

List<TreeViewModel> result = null;

if (prefixes.Any())
{
result = new List<TreeViewModel>();

foreach (var prefix in prefixes)
{
var newPrefix = !string.IsNullOrEmpty(prefixObj) ? prefixObj + "\\" + prefix : prefix;

var tree = new TreeViewModel
{
ObjectId = newPrefix,
ContainerId = containerId,
Text = prefix
};

var prefixedObjs = objectIds.Where(x => x.StartsWith(prefix + "\\")).Select(x => x.Split(new[] { '\\' }, 2)[1]).ToList();

tree.Nodes = GetObjectBranch(containerId, newPrefix, prefixedObjs);

if (tree.Nodes == null) { tree.IsFile = true; }

result.Add(tree);
}
}

return result;

}
}
}
Loading

0 comments on commit 1846feb

Please sign in to comment.