/// <summary> /// 上传文件 /// </summary> /// <param name="groupName"></param> /// <param name="content"></param> /// <param name="fileExt">后缀</param> /// <returns>文件名</returns> public static string Upload(string groupName, byte[] content, string fileExt) { StorageNodeInfo storage = GetAvailableStorageNode(groupName); if (fileExt.StartsWith(".")) { fileExt = fileExt.TrimStart(new char[] { '.' }); } return(new StorageCmds().UPLOAD_FILE(storage.EndPoint, storage.StorePathIndex, content.Length, fileExt, content)); }
/// <summary> /// query which storage server to update the file /// /// Reqeust /// Cmd: TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE 103 /// Body: /// @ FDFS_GROUP_NAME_MAX_LEN bytes: group name /// @ filename bytes: filename /// Response /// Cmd: TRACKER_PROTO_CMD_RESP /// Status: 0 right other wrong /// Body: /// @ FDFS_GROUP_NAME_MAX_LEN bytes: group name /// @ IP_ADDRESS_SIZE - 1 bytes: storage server ip address /// @ FDFS_PROTO_PKG_LEN_SIZE bytes: storage server port /// </summary> public StorageNodeInfo QUERY_UPDATE(string groupName, string serverFileName) { if (groupName.Length > Consts.FDFS_GROUP_NAME_MAX_LEN) { throw new Exception("GroupName is too long"); } //连接 Connection connection = ConnectionManager.GetTrackerConnection(); //请求头和体 var fileNameBuffer = Helper.StringToByte(serverFileName); int[] lengthes = new int[] { Consts.FDFS_GROUP_NAME_MAX_LEN, fileNameBuffer.Length }; IList <byte[]> contents = new byte[][] { Helper.StringToByte(groupName), fileNameBuffer }; Request req = new Request(); req.SetBody(lengthes, contents); req.Header = new Header(lengthes.Sum(), Consts.TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE, 0); byte[] res = req.Invoke(connection); //解析结果 StorageNodeInfo node = new StorageNodeInfo(); //byte[] groupNameBuffer = new byte[Consts.FDFS_GROUP_NAME_MAX_LEN]; //Array.Copy(res, groupNameBuffer, Consts.FDFS_GROUP_NAME_MAX_LEN); node.GroupName = groupName; //Helper.ByteToString(groupNameBuffer).TrimEnd('\0'); byte[] ipAddressBuffer = new byte[Consts.IP_ADDRESS_SIZE - 1]; Array.Copy(res, Consts.FDFS_GROUP_NAME_MAX_LEN, ipAddressBuffer, 0, Consts.IP_ADDRESS_SIZE - 1); string strIp = new string(Config.Charset.GetChars(ipAddressBuffer)).TrimEnd('\0'); byte[] portBuffer = new byte[Consts.FDFS_PROTO_PKG_LEN_SIZE]; Array.Copy(res, Consts.FDFS_GROUP_NAME_MAX_LEN + Consts.IP_ADDRESS_SIZE - 1, portBuffer, 0, Consts.FDFS_PROTO_PKG_LEN_SIZE); int intPort = (int)Helper.BufferToLong(portBuffer, 0); node.StorePathIndex = res[res.Length - 1]; node.EndPoint = new IPEndPoint(IPAddress.Parse(strIp), intPort); return(node); }