Exemplo n.º 1
0
        public UploadResult Upload(UploadConfig config)
        {
            if (string.IsNullOrEmpty(config.PreviosName))
            {
                var fileName    = string.Empty;
                var storageNode = _client.GetStorageNode(config.GroupName);

                if (!config.Chunked)
                {
                    fileName = _client.UploadFile(storageNode, config.Buffer, Path.GetExtension(config.FileName));
                }
                else
                {
                    //分段上传需要调用这个方法
                    fileName = _client.UploadAppenderFile(storageNode, config.Buffer, Path.GetExtension(config.FileName));
                }

                return(new UploadResult {
                    FilePath = fileName, OriginalName = config.FileName
                });
            }
            else
            {
                //分段上传:需要提供上传GroupName, 文件上传地址PreviosName,文件上传内容filebody
                //续传 地址config.PreviosName
                _client.AppendFile(config.GroupName, config.PreviosName, config.Buffer);

                return(new UploadResult {
                    FilePath = config.PreviosName, OriginalName = config.FileName
                });
            }
        }
Exemplo n.º 2
0
        public void GetStorageNode(string groupName)
        {
            var storageNode   = FastDfsClient.GetStorageNode(groupName);
            var endPoint      = storageNode.EndPoint;
            var nodeGroupName = storageNode.GroupName;

            Assert.Contains(endPoint.Address.ToString(),
                            new List <string> {
                "192.168.78.153", "192.168.78.154", "192.168.78.155"
            });
            Assert.Equal(nodeGroupName, groupName);
        }
Exemplo n.º 3
0
        public void FastDfsOperation(string groupName, string filePath, string fileExt)
        {
            var storageNode = FastDfsClient.GetStorageNode(groupName);
            var uploadFile  = File.ReadAllBytes(filePath);

            var fileName = _client.UploadFile(storageNode, uploadFile, fileExt);

            var downloadFile = _client.DownloadFile(storageNode, fileName);

            _client.RemoveFile(groupName, fileName);

            Assert.Equal(Md5Helper.Get32Md5(uploadFile), Md5Helper.Get32Md5(downloadFile));
        }
        public string UploadFile(byte[] fileBytes, string fileName)
        {
            var strArr = fileName.Split('.');
            if (strArr.Length < 2)
                throw new ArgumentException(nameof(fileName));
            var fileNameBuilder = new StringBuilder();
            for (var i = 0; i < strArr.Length - 1; i++)
                fileNameBuilder.Append(strArr[i]);
            var file = fileNameBuilder.ToString();
            var fileExt = strArr.Last();
            if (string.IsNullOrWhiteSpace(file) || string.IsNullOrWhiteSpace(fileExt))
                throw new ArgumentException(nameof(fileName));

            var storageNode = _fastDfsClient.GetStorageNode(_groupName);
            var fileDfsInfo = new FileDfsInfo
            {
                Id = Guid.NewGuid(),
                DfsFileName = _fastDfsClient.UploadFile(storageNode, fileBytes, fileExt),
                FileName = fileName,
                UtcCreateTime = DateTime.Now
            };
            _repository?.Add(fileDfsInfo);
            return fileDfsInfo.DfsFileName;
        }
Exemplo n.º 5
0
        public FastDFSApiClient(IConfiguration configuration)
        {
            this.configuration = configuration;

            var ip    = configuration[$"FastDFS:IP"].Replace("{WebServerIP}", Modobay.AppManager.WebServerIP);
            var port  = int.Parse(configuration[$"FastDFS:Port"]);
            var group = configuration[$"FastDFS:Group"];

            client = new FastDfsClient(new List <IPEndPoint>
            {
                new IPEndPoint(IPAddress.Parse(ip), port)
            });

            try
            {
                storageNode = FastDfsClient.GetStorageNode("group1");
            }
            catch (Exception ex) { Lib.Log.WriteExceptionLog($"FastDFSApiClient:Message:{ex.Message}  <br> StackTrace:{ex.StackTrace}"); }
        }
Exemplo n.º 6
0
 public void GetNotExistNode(string groupName)
 {
     Assert.Throws <FdfsException>(() => { FastDfsClient.GetStorageNode(groupName); });
 }
Exemplo n.º 7
0
        public void DownloadNotExistFile(string groupName, string fileName)
        {
            var storageNode = FastDfsClient.GetStorageNode(groupName);

            Assert.Null(_client.DownloadFile(storageNode, fileName));
        }