public async Task <IActionResult> Delete([FromBody] PostData postData) { // Delete file from private storage IdentityHandler.InternalIdentity identity = await IdentityHandler.GetIdentityAsync(postData.clientId, postData.container); if (identity == null) { Response.StatusCode = 403; return(Json("Could not retrieve Identity data")); } if (!identity.Data.allowFileDelete) { Response.StatusCode = 403; return(Json("No delete-access")); } BlobContainerClient azureContainerClient = new BlobContainerClient(Startup.Configuration.GetConnectionString("StorageConnectionString"), Startup.Configuration.GetValue <string>("StorageContainer")); var tkContainer = await GetStorageContainerData(azureContainerClient, postData.container); if (tkContainer == null) { Response.StatusCode = 404; return(Json("Not found")); } var blobFile = azureContainerClient.GetBlobClient(tkContainer.Code + "/" + postData.fileName); await blobFile.DeleteIfExistsAsync(); return(Json("OK")); }
public async Task <IActionResult> Retrieve([FromBody] PostData postData) { // Retrieve static url to private storage data (and meta data) IdentityHandler.InternalIdentity identity = await IdentityHandler.GetIdentityAsync(postData.clientId, postData.container); if (identity == null) { Response.StatusCode = 403; return(Json("Could not retrieve Identity data")); } if (!identity.Data.allowFileRead) { Response.StatusCode = 403; return(Json("No read-access")); } BlobContainerClient azureContainerClient = new BlobContainerClient(Startup.Configuration.GetConnectionString("StorageConnectionString"), Startup.Configuration.GetValue <string>("StorageContainer")); var tkContainer = await GetStorageContainerData(azureContainerClient, postData.container); if (tkContainer == null) { Response.StatusCode = 404; return(Json("Not found")); } var blobFile = azureContainerClient.GetBlobClient(tkContainer.Code + "/" + postData.fileName); var exists = await blobFile.ExistsAsync(); if (!exists.Value) { Response.StatusCode = 404; return(Json("Not found")); } if (postData.directly) { var downloadStream = await blobFile.OpenReadAsync(); return(File(downloadStream, GetContentType(blobFile.Name))); } return(Json(new { FileName = GetFileNameFromBlobName(blobFile.Name, true), UploadName = GetFileNameFromBlobName(blobFile.Name), Url = Startup.Configuration.GetValue <string>("StorageUrlPrefix") + blobFile.Name, ContentType = GetContentType(blobFile.Name) })); }
public async Task <IActionResult> List([FromBody] PostData postData) { // Retrieve all files from private storage IdentityHandler.InternalIdentity identity = await IdentityHandler.GetIdentityAsync(postData.clientId, postData.container); if (identity == null) { Response.StatusCode = 403; return(Json("Could not retrieve Identity data")); } if (!identity.Data.allowFileList) { Response.StatusCode = 403; return(Json("No list-access")); } if (!identity.Data.allowFileRead) { Response.StatusCode = 403; return(Json("No read-access")); } BlobContainerClient azureContainerClient = new BlobContainerClient(Startup.Configuration.GetConnectionString("StorageConnectionString"), Startup.Configuration.GetValue <string>("StorageContainer")); var tkContainer = await GetStorageContainerData(azureContainerClient, postData.container); if (tkContainer == null) { Response.StatusCode = 404; return(Json("Not found")); } var files = new List <object>(); await foreach (var uploadedBlob in azureContainerClient.GetBlobsAsync(Azure.Storage.Blobs.Models.BlobTraits.Metadata, Azure.Storage.Blobs.Models.BlobStates.None, tkContainer.Code + "/")) { files.Add(new { FileName = GetFileNameFromBlobName(uploadedBlob.Name, true), UploadName = GetFileNameFromBlobName(uploadedBlob.Name), Url = Startup.Configuration.GetValue <string>("StorageUrlPrefix") + uploadedBlob.Name, ContentType = GetContentType(uploadedBlob.Name), UploadedBy = uploadedBlob.Metadata.ContainsKey("UploadedByIdentifier") ? uploadedBlob.Metadata["UploadedByIdentifier"] : null }); } return(Json(files)); }
public async Task <IActionResult> Store(string clientId, string container = null, string fileName = null, DateTime?expirationDate = null, List <IFormFile> files = null) { if (files.Count == 0) { return(Json("No files uploaded")); } string origFileName = null; IdentityHandler.InternalIdentity identity = null; // Store a file and return static url to data if (!string.IsNullOrEmpty(container)) { // Private container identity = await IdentityHandler.GetIdentityAsync(clientId, container); if (identity == null) { Response.StatusCode = 403; return(Json("Could not retrieve Identity data")); } if (!identity.Data.allowFileWrite) { Response.StatusCode = 403; return(Json("No write-access")); } } else { // Public 'container'. This generates a random file name and will put it in a container which cannot be listed/deleted container = "public"; origFileName = fileName ?? files.FirstOrDefault()?.FileName; fileName = Guid.NewGuid().ToString(); } // Upload BlobContainerClient azureContainerClient = new BlobContainerClient(Startup.Configuration.GetConnectionString("StorageConnectionString"), Startup.Configuration.GetValue <string>("StorageContainer")); var tkContainer = await GetStorageContainerData(azureContainerClient, container, true); if (tkContainer == null) { Response.StatusCode = 500; return(Json("Internal error")); } var filesUploaded = new List <object>(); foreach (var file in files) { if (container == "public" || (string.IsNullOrEmpty(fileName) && string.IsNullOrEmpty(file.FileName))) { fileName = GetRandomString(); // Generate a random short code as filename } else if (string.IsNullOrEmpty(fileName) && !string.IsNullOrEmpty(file.FileName)) { fileName = file.FileName; } fileName = fileName.Replace("\\", "").Replace("\"", "").Replace("..", "").Replace(":", ""); if (fileName.StartsWith("/")) { fileName = fileName.Substring(1); } var uploadFileName = tkContainer.Code + "/" + GetRandomString() + "_" + fileName; var fileBlob = azureContainerClient.GetBlobClient(uploadFileName); var ext = Path.GetExtension(fileName).ToLower(); var headers = new Azure.Storage.Blobs.Models.BlobHttpHeaders(); headers.ContentDisposition = "inline"; headers.CacheControl = "public, max-age=31536000"; var contentType = GetContentType(origFileName ?? fileName); if (contentType == null) { // Only allow to download this file headers.ContentDisposition = "attachment; filename=\"" + fileName + "\""; headers.ContentType = "application/octet-stream"; } else { headers.ContentType = contentType; } using (var fileStream = file.OpenReadStream()) { await fileBlob.UploadAsync(fileStream, new Azure.Storage.Blobs.Models.BlobUploadOptions() { HttpHeaders = headers, Metadata = new Dictionary <string, string> { { "UploadedByClientId", clientId }, { "UploadedByIdentifier", identity?.Data?.publicIdentifier ?? "- Unknown -" }, { "UploadedByIP", Request.HttpContext.Connection.RemoteIpAddress.ToString() } } }); } filesUploaded.Add(new { FileName = fileName, Url = Startup.Configuration.GetValue <string>("StorageUrlPrefix") + uploadFileName, ContentType = headers.ContentType }); fileName = null; // We only allow a custom filename passed by the first upload } return(Json(filesUploaded)); }