public virtual async Task ValidationAsync(UploadOssObjectInput input)
        {
            var validation = await GetByCacheItemAsync();

            if (validation.SizeLimit * 1024 * 1024 < input.TotalSize)
            {
                throw new UserFriendlyException(_stringLocalizer["UploadFileSizeBeyondLimit", validation.SizeLimit]);
            }
            var fileExtensionName = FileHelper.GetExtension(input.FileName);

            if (!validation.AllowedExtensions
                .Any(fe => fe.Equals(fileExtensionName, StringComparison.CurrentCultureIgnoreCase)))
            {
                throw new UserFriendlyException(_stringLocalizer["NotAllowedFileExtensionName", fileExtensionName]);
            }
        }
Esempio n. 2
0
        public virtual async Task UploadAsync([FromForm] UploadOssObjectInput input)
        {
            await FileValidater.ValidationAsync(input);

            // 以上传的文件名创建一个临时目录
            var tempFilePath = Path.Combine(
                Path.GetTempPath(),
                "lingyun-abp-application",
                "upload-tmp",
                string.Concat(input.Path ?? "", input.FileName).ToMd5());

            DirectoryHelper.CreateIfNotExists(tempFilePath);
            // 以上传的分片索引创建临时文件
            var tempSavedFile = Path.Combine(tempFilePath, $"{input.ChunkNumber}.uploadtmp");

            try
            {
                if (HttpContext.RequestAborted.IsCancellationRequested)
                {
                    // 如果取消请求,删除临时目录
                    Directory.Delete(tempFilePath, true);
                    return;
                }

                if (input.File != null)
                {
                    // 保存临时文件
                    using (var fs = new FileStream(tempSavedFile, FileMode.Create, FileAccess.Write))
                    {
                        // 写入当前分片文件
                        await input.File.CopyToAsync(fs);
                    }
                }

                if (input.ChunkNumber == input.TotalChunks)
                {
                    var createOssObjectInput = new CreateOssObjectInput
                    {
                        Bucket = input.Bucket,
                        Path   = input.Path,
                        Object = input.FileName
                    };
                    // 合并文件
                    var mergeSavedFile = Path.Combine(tempFilePath, $"{input.FileName}");
                    // 获取并排序所有分片文件
                    var mergeFiles = Directory.GetFiles(tempFilePath).OrderBy(f => f.Length).ThenBy(f => f);
                    // 创建临时合并文件
                    using (var memoryStream = new MemoryStream())
                    {
                        foreach (var mergeFile in mergeFiles)
                        {
                            // 读取当前文件字节
                            var mergeFileBytes = await FileHelper.ReadAllBytesAsync(mergeFile);

                            // 写入到合并文件流
                            await memoryStream.WriteAsync(mergeFileBytes, HttpContext.RequestAborted);

                            Array.Clear(mergeFileBytes, 0, mergeFileBytes.Length);
                            // 删除已参与合并的临时文件分片
                            FileHelper.DeleteIfExists(mergeFile);
                        }
                        createOssObjectInput.SetContent(memoryStream);

                        await OssObjectAppService.CreateAsync(createOssObjectInput);

                        // 文件保存之后删除临时文件目录
                        Directory.Delete(tempFilePath, true);
                    }
                }
            }
            catch
            {
                // 发生异常删除临时文件目录
                Directory.Delete(tempFilePath, true);
                throw;
            }
        }