Esempio n. 1
0
        public override FDFSRequest GetRequest(params object[] paramList)
        {
            if (paramList.Length != 3)
            {
                throw new FDFSException("param count is wrong");
            }

            StorageNode storageNode = (StorageNode)paramList[0];
            string      fileExt     = (string)paramList[1] ?? string.Empty;

            if (fileExt.Length > 0 && fileExt[0] == '.')
            {
                fileExt = fileExt.Substring(1);
            }
            var contentStream = (Stream)paramList[2];

            var contentByteLength = contentStream.Length;

            UPLOAD_FILE uploadFile = new UPLOAD_FILE
            {
                ConnectionType = FDFSConnectionType.StorageConnection,
                EndPoint       = storageNode.EndPoint
            };

            if (fileExt.Length > 6)
            {
                throw new FDFSException("file ext is too long");
            }

            const int bodyBufferLen = 15;

            uploadFile.SetBodyBuffer(bodyBufferLen);

            int offset = 0;

            uploadFile.BodyBuffer[offset++] = storageNode.StorePathIndex;

            Util.LongToBuffer(contentByteLength, uploadFile.BodyBuffer, offset);
            offset += 8;

            var fileExtByteCount = Util.StringByteCount(fileExt);

            Util.StringToByte(fileExt, uploadFile.BodyBuffer, offset, fileExtByteCount);
            if (fileExtByteCount < 6)
            {
                for (var i = offset + fileExtByteCount; i < offset + 6; i++)
                {
                    uploadFile.BodyBuffer[i] = 0;
                }
            }

            uploadFile.BodyStream = contentStream;

            long length = bodyBufferLen + contentStream.Length;

            uploadFile.Header = new FDFSHeader(length, FDFSConstants.STORAGE_PROTO_CMD_UPLOAD_FILE, 0);
            return(uploadFile);
        }
Esempio n. 2
0
        public override FDFSRequest GetRequest(params object[] paramList)
        {
            if (paramList.Length != 3)
            {
                throw new FDFSException("param count is wrong");
            }

            StorageNode storageNode = (StorageNode)paramList[0];
            string      fileExt     = (string)paramList[1];

            byte[] contentByte = (byte[])paramList[2];

            int contentByteLength = contentByte.Length;

            UPLOAD_FILE uploadFile = new UPLOAD_FILE
            {
                ConnectionType = FDFSConnectionType.StorageConnection,
                EndPoint       = storageNode.EndPoint
            };

            if (fileExt.Length > 6)
            {
                throw new FDFSException("file ext is too long");
            }

            int length = 15 + contentByteLength;

            uploadFile.SetBodyBuffer(length);

            int offset = 0;

            uploadFile.BodyBuffer[offset++] = storageNode.StorePathIndex;

            Util.LongToBuffer(contentByteLength, uploadFile.BodyBuffer, offset);
            offset += 8;

            var fileExtByteCount = Util.StringByteCount(fileExt);

            Util.StringToByte(fileExt, uploadFile.BodyBuffer, offset, fileExtByteCount);
            if (fileExtByteCount < 6)
            {
                for (var i = offset + fileExtByteCount; i < offset + 6; i++)
                {
                    uploadFile.BodyBuffer[i] = 0;
                }
            }

            offset += 6;
            Array.Copy(contentByte, 0, uploadFile.BodyBuffer, offset, contentByte.Length);

            uploadFile.Header = new FDFSHeader(length, FDFSConstants.STORAGE_PROTO_CMD_UPLOAD_FILE, 0);
            return(uploadFile);
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="paramList">
        /// 1,IPEndPoint     IPEndPoint-->the storage IPEndPoint
        /// 2,Byte           StorePathIndex
        /// 3,long           FileSize
        /// 4,string         File Ext
        /// 5,byte[FileSize] File Content or FileStream
        /// </param>
        /// <returns></returns>
        public override FDFSRequest GetRequest(params object[] paramList)
        {
            if (paramList.Length != 5)
            {
                throw new FDFSException("param count is wrong");
            }
            IPEndPoint endPoint = (IPEndPoint)paramList[0];

            byte   storePathIndex = (byte)paramList[1];
            long   fileSize       = (long)paramList[2];
            string ext            = (string)paramList[3];

            Stream stream = paramList[4] as Stream;

            if (paramList[4] is byte[])
            {
                stream = new MemoryStream((byte[])paramList[4]);
            }

            #region 拷贝后缀扩展名值
            byte[] extBuffer    = new byte[Consts.FDFS_FILE_EXT_NAME_MAX_LEN];
            byte[] bse          = Util.StringToByte(ext);
            int    ext_name_len = bse.Length;
            if (ext_name_len > Consts.FDFS_FILE_EXT_NAME_MAX_LEN)
            {
                ext_name_len = Consts.FDFS_FILE_EXT_NAME_MAX_LEN;
            }
            Array.Copy(bse, 0, extBuffer, 0, ext_name_len);
            #endregion

            UPLOAD_FILE request = new UPLOAD_FILE();
            request.Connection = ConnectionManager.GetStorageConnection(endPoint);
            if (ext.Length > Consts.FDFS_FILE_EXT_NAME_MAX_LEN)
            {
                throw new FDFSException("file ext is too long");
            }

            long headerLength = 1 + Consts.FDFS_PROTO_PKG_LEN_SIZE + Consts.FDFS_FILE_EXT_NAME_MAX_LEN;

            byte[] bodyBuffer = new byte[headerLength];
            bodyBuffer[0] = storePathIndex;

            byte[] fileSizeBuffer = Util.LongToBuffer(fileSize);
            Array.Copy(fileSizeBuffer, 0, bodyBuffer, 1, fileSizeBuffer.Length);
            Array.Copy(extBuffer, 0, bodyBuffer, 1 + Consts.FDFS_PROTO_PKG_LEN_SIZE, extBuffer.Length);

            request.Stream = stream;
            request.Body   = bodyBuffer;
            request.Header = new FDFSHeader(headerLength + stream.Length, Consts.STORAGE_PROTO_CMD_UPLOAD_FILE, 0);
            return(request);
        }
Esempio n. 4
0
 /// <summary>
 /// 上传文件
 /// </summary>
 /// <param name="storageNode">GetStorageNode方法返回的存储节点</param>
 /// <param name="contentByte">文件内容</param>
 /// <param name="fileExt">文件扩展名(注意:不包含".")</param>
 /// <returns>文件名</returns>
 public static string UploadFile(StorageNode storageNode, byte[] contentByte, string fileExt)
 {
     Console.WriteLine($"{DateTime.Now.ToString("yyyyMMdd hh:mm:ss:fff")} => start upload fastdfs = > {storageNode.EndPoint.Address.ToString()} => contentByte.length= {contentByte.Length}");
     using (var storageReqeust = UPLOAD_FILE.CreateRequest(
                storageNode.EndPoint,
                storageNode.StorePathIndex,
                contentByte.Length,
                fileExt,
                contentByte))
     {
         var storageResponse = new UPLOAD_FILE.Response(storageReqeust.GetStorageResponse());
         Console.WriteLine($"{DateTime.Now.ToString("yyyyMMdd hh:mm:ss:fff")} => sucess upload fastdfs = > {storageNode.EndPoint.Address.ToString()} => {storageResponse.FileName}");
         return(storageResponse.FileName);
     }
 }
Esempio n. 5
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="paramList">
        /// 1,IPEndPoint     IPEndPoint-->the storage IPEndPoint
        /// 2,Byte           StorePathIndex
        /// 3,long           FileSize
        /// 4,string         File Ext
        /// 5,byte[FileSize] File Content or FileStream
        /// </param>
        /// <returns></returns>
        public override FDFSRequest GetRequest(params object[] paramList)
        {
            if (paramList.Length != 5)
                throw new FDFSException("param count is wrong");
            IPEndPoint endPoint = (IPEndPoint)paramList[0];
            
            byte storePathIndex = (byte)paramList[1];
            long fileSize = long.Parse(paramList[2].ToString());
            string ext = (string)paramList[3];
            
            Stream stream = paramList[4] as Stream;
            if (paramList[4] is byte[])
            {
                stream = new MemoryStream((byte[])paramList[4]);
            }

            #region 拷贝后缀扩展名值
            byte[] extBuffer = new byte[Consts.FDFS_FILE_EXT_NAME_MAX_LEN];
            byte[] bse = Util.StringToByte(ext);
            int ext_name_len = bse.Length;
            if (ext_name_len > Consts.FDFS_FILE_EXT_NAME_MAX_LEN)
            {
                ext_name_len = Consts.FDFS_FILE_EXT_NAME_MAX_LEN;
            }
            Array.Copy(bse, 0, extBuffer, 0, ext_name_len);
            #endregion
            
            UPLOAD_FILE request = new UPLOAD_FILE();
            request.Connection = ConnectionManager.GetStorageConnection(endPoint);
            if(ext.Length>Consts.FDFS_FILE_EXT_NAME_MAX_LEN)
                throw new FDFSException("file ext is too long");

            long headerLength = 1 + Consts.FDFS_PROTO_PKG_LEN_SIZE + Consts.FDFS_FILE_EXT_NAME_MAX_LEN;

            byte[] bodyBuffer = new byte[headerLength];
            bodyBuffer[0] = storePathIndex;

            byte[] fileSizeBuffer = Util.LongToBuffer(fileSize);
            Array.Copy(fileSizeBuffer, 0, bodyBuffer, 1, fileSizeBuffer.Length);
            Array.Copy(extBuffer, 0, bodyBuffer, 1 + Consts.FDFS_PROTO_PKG_LEN_SIZE, extBuffer.Length);
            
            request.Stream = stream;
            request.Body = bodyBuffer;
            request.Header = new FDFSHeader(headerLength + stream.Length, Consts.STORAGE_PROTO_CMD_UPLOAD_FILE, 0);
            return request;
        }
Esempio n. 6
0
        public override FDFSRequest GetRequest(params object[] paramList)
        {
            if (paramList.Length != 5)
            {
                throw new FDFSException("param count is wrong");
            }
            IPEndPoint endPoint = (IPEndPoint)paramList[0];
            byte       num      = (byte)paramList[1];
            int        num2     = (int)paramList[2];
            string     input    = (string)paramList[3];

            byte[] sourceArray      = (byte[])paramList[4];
            byte[] destinationArray = new byte[6];
            byte[] buffer3          = Util.StringToByte(input);
            int    length           = buffer3.Length;

            if (length > 6)
            {
                length = 6;
            }
            Array.Copy(buffer3, 0, destinationArray, 0, length);
            UPLOAD_FILE uploadFile = new UPLOAD_FILE
            {
                ConnectionType = 1,
                EndPoint       = endPoint
            };

            if (input.Length > 6)
            {
                throw new FDFSException("file ext is too long");
            }
            long num4 = 15 + sourceArray.Length;

            byte[] buffer4 = new byte[num4];
            buffer4[0] = num;
            byte[] buffer5 = Util.LongToBuffer((long)num2);
            Array.Copy(buffer5, 0, buffer4, 1, buffer5.Length);
            Array.Copy(destinationArray, 0, buffer4, 9, destinationArray.Length);
            Array.Copy(sourceArray, 0, buffer4, 15, sourceArray.Length);
            uploadFile.Body   = buffer4;
            uploadFile.Header = new FDFSHeader(num4, 11, 0);
            return(uploadFile);
        }