Example #1
0
        /// <summary>
        /// �ϴ��ļ�.
        /// </summary>
        /// <param name="groupName">�ϴ�������������.</param>
        /// <param name="buffer">�ϴ��ļ��Ķ�������.</param>
        /// <param name="extension">�ϴ��ļ�����չ��.</param>
        /// <param name="metadatas">�ļ�����չ����.</param>
        /// <returns>�ļ����ڵ�·��</returns>
        public static string Upload(string groupName, byte[] buffer, string extension, NameValuePair[] metadatas)
        {
            string[] uploadPath = DoUpload(groupName, string.Empty, buffer, extension, metadatas);
            //return uploadPath != null ? Util.GetFilePath(string.Format("{0}{1}{2}", uploadPath[0], SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR, uploadPath[1])) : null;

            return uploadPath != null ? Util.GetFilePath(string.Format("{0}{1}", SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR, uploadPath[1])) : null;
        }
Example #2
0
        /// <summary>
        /// 将元数据和转换成协议规定格式
        /// </summary>
        /// <param name="nameValuePair">The name value pair.</param>
        /// <returns></returns>
        public static string PackMetadata(NameValuePair[] nameValuePair)
        {
            if (nameValuePair.Length == 0)
            {
                return "";
            }

            StringBuilder sb = new StringBuilder(32*nameValuePair.Length);
            sb.Append(nameValuePair[0].Name).Append(Protocol.FDFS_FIELD_SEPERATOR).Append(nameValuePair[0].Value);
            for (int i = 1; i < nameValuePair.Length; i++)
            {
                sb.Append(Protocol.FDFS_RECORD_SEPERATOR);
                sb.Append(nameValuePair[i].Name).Append(Protocol.FDFS_FIELD_SEPERATOR).Append(nameValuePair[i].Value);
            }

            return sb.ToString();
        }
Example #3
0
        /// <summary>
        /// 取得元数据.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="recordSeperator">The record seperator.</param>
        /// <param name="filedSeperator">The filed seperator.</param>
        /// <returns></returns>
        public static NameValuePair[] SplitMetadata(string buffer,
                                                    string recordSeperator, string filedSeperator)
        {
            string[] cols;
            NameValuePair[] nameValuePairs;

            string[] rows = buffer.Split(new string[] {recordSeperator}, StringSplitOptions.RemoveEmptyEntries);
            nameValuePairs = new NameValuePair[rows.Length];
            for (int i = 0; i < rows.Length; i++)
            {
                cols = rows[i].Split(new string[] {filedSeperator}, 2, StringSplitOptions.RemoveEmptyEntries);
                nameValuePairs[i] = new NameValuePair(cols[0]);
                if (cols.Length == 2)
                {
                    nameValuePairs[i].Value = (cols[1]);
                }
            }

            return nameValuePairs;
        }
        /// <summary>
        /// 上传文件.
        /// </summary>
        /// <param name="groupName">Name of the group.</param>
        /// <param name="localFileName">Name of the local file.</param>
        /// <param name="buffer">The buffer.</param>
        /// <param name="extension">The extension.</param>
        /// <param name="metadatas">The metadatas.</param>
        /// <returns></returns>
        protected static string[] DoUpload(string groupName, string localFileName, byte[] buffer,
                                                     string extension, NameValuePair[] metadatas)
        {
            TcpConnection storageConnection = GetStorageConnection(groupName);
            if (null != _logger)
                _logger.InfoFormat("Storage服务器的IP是:{0}.端口为{1}", storageConnection.IpAddress, storageConnection.Port);
            long totalBytes = 0;
            try
            {
                long length;
                FileStream stream;

                byte[] metadatasBuffer = metadatas == null
                                              ? new byte[0]
                                              : Encoding.GetEncoding(FastDFSService.Charset).GetBytes(Util.PackMetadata(metadatas));

                byte[] bufferSize = new byte[1 + 2 * Protocol.TRACKER_PROTO_PKG_LEN_SIZE];

                if (!string.IsNullOrEmpty(localFileName))
                {
                    FileInfo fileInfo = new FileInfo(localFileName);
                    length = fileInfo.Exists ? fileInfo.Length : 0;
                    stream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read);
                }
                else
                {
                    length = buffer.Length;
                    stream = null;
                }

                byte[] extensionBuffer = new byte[Protocol.FDFS_FILE_EXT_NAME_MAX_LEN];
                Util.InitializeBuffer(extensionBuffer, 0);
                if (!string.IsNullOrEmpty(extension))
                {
                    byte[] bs = Encoding.GetEncoding(FastDFSService.Charset).GetBytes(extension);
                    int ext_name_len = bs.Length;
                    if (ext_name_len > Protocol.FDFS_FILE_EXT_NAME_MAX_LEN) ext_name_len = Protocol.FDFS_FILE_EXT_NAME_MAX_LEN;
                    Array.Copy(bs, 0, extensionBuffer, 0, ext_name_len);
                }

                Util.InitializeBuffer(bufferSize, 0);
                bufferSize[0] = (byte)storageConnection.Index;
                byte[] hexBuffer = Util.LongToBuffer(metadatasBuffer.Length);
                Array.Copy(hexBuffer, 0, bufferSize, 1, hexBuffer.Length);
                hexBuffer = Util.LongToBuffer(length);
                Array.Copy(hexBuffer, 0, bufferSize, 1 + Protocol.TRACKER_PROTO_PKG_LEN_SIZE, hexBuffer.Length);

                byte[] header = Util.PackHeader(Protocol.STORAGE_PROTO_CMD_UPLOAD_FILE,
                                                1 + 2 * Protocol.TRACKER_PROTO_PKG_LEN_SIZE +
                                                Protocol.FDFS_FILE_EXT_NAME_MAX_LEN + metadatasBuffer.Length + length,
                                                0);
                Stream outStream = storageConnection.GetStream();
                outStream.Write(header, 0, header.Length);
                outStream.Write(bufferSize, 0, bufferSize.Length);
                outStream.Write(extensionBuffer, 0, extensionBuffer.Length);
                outStream.Write(metadatasBuffer, 0, metadatasBuffer.Length);
                if (stream != null)
                {
                    int readBytes;
                    byte[] buff = new byte[128 * 1024];

                    while ((readBytes = Util.ReadInput(stream, buff, 0, buff.Length)) >= 0)
                    {
                        if (readBytes == 0) continue;
                        outStream.Write(buff, 0, readBytes);
                        totalBytes += readBytes;
                    }
                }
                else
                {
                    outStream.Write(buffer, 0, buffer.Length);
                }

                PackageInfo pkgInfo = Util.RecvPackage(storageConnection.GetStream(), Protocol.STORAGE_PROTO_CMD_RESP, -1);
                if (pkgInfo.ErrorNo != 0) return null;

                if (pkgInfo.Body.Length <= Protocol.FDFS_GROUP_NAME_MAX_LEN)
                    throw new Exception(string.Format("_body length: {0} <= {1}",
                                                      pkgInfo.Body.Length, Protocol.FDFS_GROUP_NAME_MAX_LEN));

                char[] chars = Util.ToCharArray(pkgInfo.Body);
                string newGroupName = new string(chars, 0, Protocol.FDFS_GROUP_NAME_MAX_LEN).Trim();
                string remoteFileName = new string(chars, Protocol.FDFS_GROUP_NAME_MAX_LEN, pkgInfo.Body.Length - Protocol.FDFS_GROUP_NAME_MAX_LEN);
                string[] results = new string[]
                                       {
                                           newGroupName, remoteFileName
                                       };
                return results;
            }
            finally
            {
                storageConnection.Close(false, true);
            }
        }
Example #5
0
 /// <summary>
 /// �ϴ��ļ�.
 /// </summary>
 /// <param name="buffer">�ϴ��ļ��Ķ�������.</param>
 /// <param name="extension">�ϴ��ļ�����չ��.</param>
 /// <param name="metadatas">�ļ�����չ����.</param>
 /// <returns>�ļ����ڵ�·��</returns>
 public static string Upload(byte[] buffer, string extension, NameValuePair[] metadatas)
 {
     return Upload(string.Empty, buffer, extension, metadatas);
 }
Example #6
0
 /// <summary>
 /// �ϴ��ļ�.
 /// </summary>
 /// <param name="localFileName">�ϴ��ļ��ı���·��.</param>
 /// <param name="extension">�ϴ��ļ�����չ��.</param>
 /// <returns>�ļ����ڵ�·��</returns>
 public static string Upload(string localFileName, string extension, NameValuePair[] metadatas)
 {
     return Upload(string.Empty, localFileName, extension, metadatas);
 }