Ejemplo n.º 1
0
        /// <summary>生成新文档,如已存在则直接返回已存在文档 wang加</summary>
        private async Task <SysFile> BuildNewCheckExists(ServerUploadRequest request, UploadResult result)
        {
            var doc       = BuildNew(request);
            var uploadMD5 = request.Md5;

            doc.Md5  = BuildMD5(request.ServerFile, uploadMD5);
            doc.Size = new FileInfo(request.ServerFile).Length;
            var existsDoc = await _fileMan.GetByMd5Async(doc.Md5);

            if (existsDoc == null)
            {
                var docFile = _uploadPath + doc.RelativePath;
                File.Move(request.ServerFile, docFile);
            }
            else //服务端去重, wang加
            {
                var docFile = _uploadPath + existsDoc.RelativePath;
                if (!File.Exists(docFile)) //万一找到的旧文件不存在,就复制新传的文件
                {
                    docFile = _uploadPath + doc.RelativePath;
                    File.Move(request.ServerFile, docFile);
                    await this._fileMan.UpdateManyAsync(new { doc.RelativePath, existsDoc.Md5 });
                }
                else //如果相同的文件已存在,则新增一条指向原有文件的记录
                {
                    doc.RelativePath = existsDoc.RelativePath;
                }
                result.Dup = true;
            }
            await _fileMan.InsertAsync(doc);

            return(doc);
        }
Ejemplo n.º 2
0
        /// <summary>秒传,需要客户端提供文件的MD5</summary>
        private async Task <UploadResult> HandleClientMD5Async(ServerUploadRequest request)
        {
            UploadResult result = new UploadResult();

            if (request.Md5.IsEmpty())
            {
                return(result);
            }
            var existsDoc = await _fileMan.GetByMd5Async(request.Md5);

            if (existsDoc != null)
            {
                var docFile = _uploadPath + existsDoc.RelativePath;
                if (File.Exists(docFile))
                {
                    //如果相同的文件已存在,则新增一条指向原有文件的记录
                    existsDoc.Id   = null;
                    existsDoc.Name = request.FileName;
                    existsDoc.Path = request.FilePath;
                    await _fileMan.InsertAsync(existsDoc);

                    result.Id = existsDoc.Id;
                }
                result.Dup = true;
            }
            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>处理单文件上传</summary>
        private async Task <UploadResult> HandleSingleAsync(ServerUploadRequest request)
        {
            UploadResult result = new UploadResult();
            var          doc    = await BuildNewCheckExists(request, result);

            result.Id = doc.Id;
            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>处理分片文件上传</summary>
        private async Task <UploadResult> HandleChunkAsync(ServerUploadRequest request)
        {
            UploadResult result = new UploadResult();

            if (request.Chunk < 0 || request.Chunks < 1 || request.Chunk > request.Chunks)
            {
                throw new Exception("分片参数无效!");
            }
            var uploadMD5 = request.Md5;

            result.Chunk = request.Chunk;
            if (request.Chunk < request.Chunks)
            {
                if (request.ServerFile.IsEmpty())
                {
                    throw new Exception("分片上传时流不存在!");
                }
                var chunkFile = GetNewChunkFile(request, request.Chunk);
                if (File.Exists(chunkFile))
                {
                    File.Delete(chunkFile);
                }
                File.Move(request.ServerFile, chunkFile);
            }
            else
            {
                #region 合并分片文件
                var           mergeFile  = GetNewMergeFile(request);
                List <string> chunkFiles = new List <string>();
                using (var mergeStream = new FileStream(mergeFile, FileMode.Create, FileAccess.Write))
                {
                    for (var i = 0; i < request.Chunks; i++)
                    {
                        var chunkFile = GetNewChunkFile(request, i);
                        using (var chunkStream = new FileStream(chunkFile, FileMode.Open, FileAccess.Read))
                        {
                            await chunkStream.CopyToAsync(mergeStream);
                        }
                        chunkFiles.Add(chunkFile);
                    }
                }
                request.ServerFile = mergeFile;
                var doc = await BuildNewCheckExists(request, result);

                result.Md5 = doc.Md5;
                result.Id  = doc.Id;
                chunkFiles.ForEach(File.Delete);
                #endregion
            }
            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>处理文件上传</summary>
        public async Task <UploadResult> UploadAsync(ServerUploadRequest request)
        {
            UploadResult result = null;

            if (!request.Md5.IsEmpty())
            {
                result = await HandleClientMD5Async(request);

                if (!result.Id.IsEmpty())
                {
                    return(result);
                }
            }
            if (request.Guid.IsEmpty())
            {
                result = await HandleSingleAsync(request);
            }
            else
            {
                result = await HandleChunkAsync(request);
            }
            return(result);
        }
Ejemplo n.º 6
0
        private SysFile BuildNew(ServerUploadRequest request)
        {
            var doc = new SysFile
            {
                Name       = request.FileName,
                CreateTime = DateTime.Now,
                UserId     = request.UserId
            };

            var ext = Path.GetExtension(request.FileName);

            var level1Dir = doc.CreateTime.ToString("yyyyMM");
            var level2Dir = doc.CreateTime.ToString("ddHH");
            var docPath   = $@"{this._uploadPath}\{level1Dir}\{level2Dir}";

            if (!Directory.Exists(docPath))
            {
                Directory.CreateDirectory(docPath);
            }
            doc.RelativePath = $@"\{level1Dir}\{level2Dir}\{Guid.NewGuid().ToString("N")}{ext}";
            doc.ContentType  = IOHelper.GetContentType(request.FileName);
            return(doc);
        }
Ejemplo n.º 7
0
        /// <summary>获得分片合并文件路径</summary>
        private string GetNewMergeFile(ServerUploadRequest request)
        {
            var ext = Path.GetExtension(request.FileName);

            return($@"{this.TempPath}\{request.Guid}{ext}");
        }
Ejemplo n.º 8
0
        /// <summary>获得新分片文件路径</summary>
        private string GetNewChunkFile(ServerUploadRequest request, int chunk)
        {
            var ext = Path.GetExtension(request.FileName);

            return($@"{this.TempPath}\{request.Guid}_PART_{chunk.ToString()}{ext}");
        }