private FileUploadResult UploadFile(IFormFile file) { if (file == null) { throw new ArgumentNullException(nameof(file)); } FileUploadResult fileUploadResult = new FileUploadResult { IsSuccessful = false }; try { string extension = Path.GetExtension(file.FileName); using (var stream = file.OpenReadStream()) { var fileId = Guid.NewGuid().ToString(); var fileName = $"{fileId}{extension}"; var fileInfo = _filesService.Add(fileId, fileName, stream); fileUploadResult.FileInfo = fileInfo; } fileUploadResult.IsSuccessful = true; } catch (Exception ex) { _logger.LogCritical(ex, "There was an error while saving the file"); } return(fileUploadResult); }
public async Task <ActionResult <ItemsResponse <BaseFile> > > Create(List <IFormFile> files) { ObjectResult result = null; try { int currentUserId = _authService.GetCurrentUserId(); List <BaseFile> urls = await _service.Add(files, currentUserId); result = Ok200(new ItemsResponse <BaseFile>() { Items = urls }); } catch (Exception ex) { Logger.LogError(ex.ToString()); ErrorResponse response = new ErrorResponse(ex.Message); result = StatusCode(500, response); } return(result); }
public async Task <IActionResult> EnableElementSharing(int userId, [FromQuery] string elementId) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } try { if (!await fsc.AccessControl.CheckPermissionsWithUsernameAsync(elementId, userId.ToString(), true, false, false, false)) { return(Unauthorized()); } var shareId = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); shareId = shareId.Replace("/", "t"); var fileInfo = await fsc.AccessControl.AuthorizeTokenAsync(elementId, shareId, true, true, true); if (fileInfo.Type == 2) { var subElems = await fsc.Directories.GetSubelementsAsync(fileInfo.ID); foreach (var item in subElems) { if (item.Type == 2) { continue; } var itemInfo = await fsc.AccessControl.AuthorizeTokenAsync(item.ID, shareId, true, true, true); await fsc.Files.SetCustomMetadataAsync(item.ID, "ShareID", String.Empty); await fsc.Files.SetCustomMetadataAsync(item.ID, "Shared", false); } } ShareMap f = new ShareMap() { ElementId = fileInfo.ID, Type = fileInfo.Type, ShareId = shareId, Active = true }; filesService.Add <ShareMap>(f); await fsc.Files.SetCustomMetadataAsync(elementId, "ShareID", shareId); await fsc.Files.SetCustomMetadataAsync(elementId, "Shared", true); if (await filesService.SaveAll()) { return(Ok(shareId)); } return(BadRequest("Failed to share element")); } catch (MDBFS.Exceptions.MdbfsElementNotFoundException) { return(NotFound("Unknown element id")); } catch (Exception) { return(BadRequest("Failed to share element")); } }