public async Task <IHttpActionResult> DownloadFile(int id, string token, int reason = 0) { var archiveRecordId = id; if (string.IsNullOrWhiteSpace(token)) { return(Content(HttpStatusCode.Forbidden, "Invalid token")); } var ipAdress = downloadHelper.GetClientIp(Request); if (!downloadTokenDataAccess.CheckTokenIsValidAndClean(token, archiveRecordId, DownloadTokenType.ArchiveRecord, ipAdress)) { return(BadRequest("Token expired or is not valid")); } var userId = downloadTokenDataAccess.GetUserIdByToken(token, archiveRecordId, DownloadTokenType.ArchiveRecord, ipAdress); if (string.IsNullOrWhiteSpace(userId)) { return(Content(HttpStatusCode.Forbidden, "No User found for the requested Downloadtoken")); } downloadTokenDataAccess.CleanUpOldToken(token, archiveRecordId, DownloadTokenType.ArchiveRecord); var access = GetUserAccessFunc(userId); var user = userDataAccess.GetUser(userId); var entityResult = elasticService.QueryForId <ElasticArchiveRecord>(archiveRecordId, access); var record = entityResult.Response?.Hits?.FirstOrDefault()?.Source; if (record == null) { return(NotFound()); } var packageId = record.PrimaryData.FirstOrDefault()?.PackageId ?? ""; if (string.IsNullOrEmpty(packageId)) { return(BadRequest("VE does not contain any primarydata and/or a valid packageid")); } if (!CheckUserHasDownloadTokensForVe(access, record)) { return(StatusCode(HttpStatusCode.Forbidden)); } try { if (reason != 0) { userDataAccess.StoreDownloadReasonInHistory(record, user, access, reason); } var downloadAssetResult = await downloadClient.Request(new DownloadAssetRequest { ArchiveRecordId = archiveRecordId.ToString(), AssetType = AssetType.Gebrauchskopie, Recipient = userId, AssetId = record.PrimaryData.FirstOrDefault()?.PackageId, RetentionCategory = await cacheHelper.GetRetentionCategory(record, access.RolePublicClient, orderDataAccess) }); var stream = cacheHelper.GetStreamFromCache(downloadAssetResult.AssetDownloadLink); var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(stream) }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = archiveRecordId + ".zip" }; await kontrollstellenInformer.InformIfNecessary(access, new[] { new VeInfo(archiveRecordId, reason) }); downloadLogDataAccess.LogVorgang(token, "Download"); return(ResponseMessage(result)); } catch (Exception e) { Log.Error(e, "(FileController:DownloadFile({ID}))", archiveRecordId); throw; } }
public async Task <IHttpActionResult> DownloadFile(int id, string token) { var orderItemId = id; if (string.IsNullOrWhiteSpace(token)) { return(Content(HttpStatusCode.Forbidden, "Invalid token")); } var ipAdress = downloadHelper.GetClientIp(Request); if (!downloadTokenDataAccess.CheckTokenIsValidAndClean(token, orderItemId, DownloadTokenType.OrderItem, ipAdress)) { return(BadRequest("Token expires or is not valid")); } var userId = downloadTokenDataAccess.GetUserIdByToken(token, orderItemId, DownloadTokenType.OrderItem, ipAdress); if (string.IsNullOrWhiteSpace(userId)) { return(Content(HttpStatusCode.Forbidden, "No User found for the requested Downloadtoken")); } var orderItem = (await orderManagerClient.FindOrderItems(new[] { orderItemId })).FirstOrDefault(); if (orderItem == null) { return(BadRequest("OrderItem does not exist in DB")); } if (!orderItem.Benutzungskopie.HasValue || !orderItem.Benutzungskopie.Value) { return(BadRequest("OrderItem is not a Benutzungskopie")); } downloadTokenDataAccess.CleanUpOldToken(token, orderItemId, DownloadTokenType.OrderItem); var downloadAssetResult = (await downloadClient.GetResponse <DownloadAssetResult>(new DownloadAssetRequest { ArchiveRecordId = orderItem.VeId.ToString(), OrderItemId = orderItemId, AssetType = AssetType.Benutzungskopie, Recipient = userId, RetentionCategory = CacheRetentionCategory.UsageCopyBenutzungskopie, ForceSendPasswordMail = true })).Message; // If item is not in cache, indicate that it is gone if (string.IsNullOrEmpty(downloadAssetResult.AssetDownloadLink)) { return(ResponseMessage(new HttpResponseMessage(HttpStatusCode.Gone))); } var stream = cacheHelper.GetStreamFromCache(downloadAssetResult.AssetDownloadLink); var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(stream) }; var fileName = orderItemId + ".zip"; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName }; return(ResponseMessage(result)); }