public async Task InvokeAsync(HttpContext context, SetupInfo setupInfo, DaoFactory daoFactory, FileSizeComment fileSizeComment, IServiceProvider serviceProvider, TenantExtra tenantExtra, TenantStatisticsProvider tenantStatisticsProvider) { context.Request.EnableBuffering(); var fileUploadResult = new FileUploadResult(); if (context.Request.Form.Files.Count == 0) { await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult)); } var fileName = context.Request.Form.Files[0].FileName; var contentLength = context.Request.Form.Files[0].Length; if (String.IsNullOrEmpty(fileName) || contentLength == 0) { throw new InvalidOperationException(CRMErrorsResource.InvalidFile); } if (0 < setupInfo.MaxUploadSize(tenantExtra, tenantStatisticsProvider) && setupInfo.MaxUploadSize(tenantExtra, tenantStatisticsProvider) < contentLength) { throw fileSizeComment.FileSizeException; } fileName = fileName.LastIndexOf('\\') != -1 ? fileName.Substring(fileName.LastIndexOf('\\') + 1) : fileName; var document = serviceProvider.GetService <File <int> >(); document.Title = fileName; document.FolderID = daoFactory.GetFileDao().GetRoot(); document.ContentLength = contentLength; document = daoFactory.GetFileDao().SaveFile(document, context.Request.Form.Files[0].OpenReadStream()); fileUploadResult.Data = document.ID; fileUploadResult.FileName = document.Title; fileUploadResult.FileURL = document.DownloadUrl; fileUploadResult.Success = true; await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult)); }
public ChunkedUploadSession <T> UploadChunk <T>(string uploadId, Stream stream, long chunkLength) { var uploadSession = ChunkedUploadSessionHolder.GetSession <T>(uploadId); uploadSession.Expired = DateTime.UtcNow + ChunkedUploadSessionHolder.SlidingExpiration; if (chunkLength <= 0) { throw new Exception(FilesCommonResource.ErrorMassage_EmptyFile); } if (chunkLength > SetupInfo.ChunkUploadSize) { throw FileSizeComment.GetFileSizeException(SetupInfo.MaxUploadSize(TenantExtra, TenantStatisticsProvider)); } var maxUploadSize = GetMaxFileSize(uploadSession.FolderId, uploadSession.BytesTotal > 0); if (uploadSession.BytesUploaded + chunkLength > maxUploadSize) { AbortUpload(uploadSession); throw FileSizeComment.GetFileSizeException(maxUploadSize); } var dao = DaoFactory.GetFileDao <T>(); dao.UploadChunk(uploadSession, stream, chunkLength); if (uploadSession.BytesUploaded == uploadSession.BytesTotal) { var linkDao = DaoFactory.GetLinkDao(); linkDao.DeleteAllLink(uploadSession.File.ID.ToString()); FileMarker.MarkAsNew(uploadSession.File); ChunkedUploadSessionHolder.RemoveSession(uploadSession); } else { ChunkedUploadSessionHolder.StoreSession(uploadSession); } return(uploadSession); }