|
@@ -18,57 +18,70 @@ type ReturnCommand interface {
|
|
|
|
|
|
|
|
func HandlerReceiveFile(info *ConnInfo, msg []byte) {
|
|
func HandlerReceiveFile(info *ConnInfo, msg []byte) {
|
|
|
var commandId = msg[0]
|
|
var commandId = msg[0]
|
|
|
-
|
|
|
|
|
//事务id解析
|
|
//事务id解析
|
|
|
- var transactionId = msg[1:33]
|
|
|
|
|
|
|
+ var transactionId = msg[1:17]
|
|
|
fmt.Println(ToHexBytes(transactionId))
|
|
fmt.Println(ToHexBytes(transactionId))
|
|
|
var tranId = ToHexBytes(transactionId)
|
|
var tranId = ToHexBytes(transactionId)
|
|
|
if commandId == 0 {
|
|
if commandId == 0 {
|
|
|
- InfoTransaction(info, msg, tranId)
|
|
|
|
|
|
|
+ InfoTransaction(info, msg, tranId, 16)
|
|
|
} else if commandId == 1 {
|
|
} else if commandId == 1 {
|
|
|
- err := InfoTransactionSplit(info, msg, tranId)
|
|
|
|
|
|
|
+ err := InfoTransactionSplit(info, msg, tranId, 16)
|
|
|
fmt.Println(err)
|
|
fmt.Println(err)
|
|
|
}
|
|
}
|
|
|
- //1. 接收基本元数据(事务id(32),len文件名(4+?),文件大小(8),相对路径(4+?),分片数量(按特定大小进行分片)(4),创建时间(8),修改时间(8),访问时间(8)=> 返回事务id,如果重复则返回错误 76+?
|
|
|
|
|
- //2. 接收包含事务id的分片内容
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// InfoTransaction 获取的事务数据内容
|
|
// InfoTransaction 获取的事务数据内容
|
|
|
-func InfoTransaction(info *ConnInfo, msg []byte, transactionId string) error {
|
|
|
|
|
-
|
|
|
|
|
|
|
+func InfoTransaction(info *ConnInfo, msg []byte, transactionId string, transactionIdLen int) error {
|
|
|
|
|
+ // 获取数据,判断事务是否注册
|
|
|
fileUser, err := db.FileUserDao{}.Get(transactionId)
|
|
fileUser, err := db.FileUserDao{}.Get(transactionId)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return errors.New("FileUser transactionId is not found")
|
|
return errors.New("FileUser transactionId is not found")
|
|
|
}
|
|
}
|
|
|
|
|
+ // 判断用户是否存在
|
|
|
if fileUser.UserId == 0 {
|
|
if fileUser.UserId == 0 {
|
|
|
return errors.New("FileUser userId is not found")
|
|
return errors.New("FileUser userId is not found")
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- var fileNameLength = binary.BigEndian.Uint32(msg[33:37])
|
|
|
|
|
- var filename = string(msg[37 : 37+fileNameLength])
|
|
|
|
|
- var fileSize = int64(binary.BigEndian.Uint64(msg[37+fileNameLength : 45+fileNameLength]))
|
|
|
|
|
- var pathLength = binary.BigEndian.Uint32(msg[45+fileNameLength : 49+fileNameLength])
|
|
|
|
|
- var path = string(msg[49+fileNameLength : 49+fileNameLength+pathLength])
|
|
|
|
|
|
|
+ var index = transactionIdLen + 1
|
|
|
|
|
+ // 文件名称长度
|
|
|
|
|
+ var fileNameLength = binary.BigEndian.Uint32(msg[index : index+4])
|
|
|
|
|
+ index += 4
|
|
|
|
|
+ // 文件名
|
|
|
|
|
+ var filename = string(msg[index : uint32(index)+fileNameLength])
|
|
|
|
|
+ index += int(fileNameLength)
|
|
|
|
|
+ // 文件大小
|
|
|
|
|
+ var fileSize = int64(binary.BigEndian.Uint64(msg[index : index+8]))
|
|
|
|
|
+ index += 8
|
|
|
|
|
+ // 路径长度
|
|
|
|
|
+ var pathLength = binary.BigEndian.Uint32(msg[index : index+4])
|
|
|
|
|
+ index += 4
|
|
|
|
|
+ // 路径
|
|
|
|
|
+ var path = string(msg[index : index+int(pathLength)])
|
|
|
|
|
+ index += int(pathLength)
|
|
|
|
|
+ // 添加专属路径 前缀
|
|
|
path = fmt.Sprint(config.Conf.File.Upload.Path, "/", fileUser.UserId, "/", transactionId, "/", path)
|
|
path = fmt.Sprint(config.Conf.File.Upload.Path, "/", fileUser.UserId, "/", transactionId, "/", path)
|
|
|
-
|
|
|
|
|
- var splitCount = binary.BigEndian.Uint32(msg[49+fileNameLength+pathLength : 53+fileNameLength+pathLength])
|
|
|
|
|
-
|
|
|
|
|
- var createTime = binary.BigEndian.Uint64(msg[53+fileNameLength+pathLength : 61+fileNameLength+pathLength])
|
|
|
|
|
- var modifyTime = binary.BigEndian.Uint64(msg[61+fileNameLength+pathLength : 69+fileNameLength+pathLength])
|
|
|
|
|
- var accessTime = binary.BigEndian.Uint64(msg[69+fileNameLength+pathLength : 77+fileNameLength+pathLength])
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // 分片数量,文件被拆分了几份
|
|
|
|
|
+ var splitCount = binary.BigEndian.Uint32(msg[index : index+4])
|
|
|
|
|
+ index += 4
|
|
|
|
|
+ // 创建时间,修改时间,访问时间
|
|
|
|
|
+ var createTime = binary.BigEndian.Uint64(msg[index : index+8])
|
|
|
|
|
+ index += 8
|
|
|
|
|
+ var modifyTime = binary.BigEndian.Uint64(msg[index : index+8])
|
|
|
|
|
+ index += 8
|
|
|
|
|
+ var accessTime = binary.BigEndian.Uint64(msg[index : index+8])
|
|
|
|
|
+ index += 8
|
|
|
Type := ""
|
|
Type := ""
|
|
|
if fileSize == -1 {
|
|
if fileSize == -1 {
|
|
|
Type = "folder"
|
|
Type = "folder"
|
|
|
} else {
|
|
} else {
|
|
|
Type = "file"
|
|
Type = "file"
|
|
|
}
|
|
}
|
|
|
|
|
+ //文件扩展名
|
|
|
ExtensionName := ""
|
|
ExtensionName := ""
|
|
|
- index := strings.Index(filename, ".")
|
|
|
|
|
- if index != -1 {
|
|
|
|
|
- ExtensionName = filename[index+1:]
|
|
|
|
|
|
|
+ suffixIndex := strings.Index(filename, ".")
|
|
|
|
|
+ if suffixIndex != -1 {
|
|
|
|
|
+ ExtensionName = filename[suffixIndex+1:]
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+ //填充数据
|
|
|
file := domain.File{
|
|
file := domain.File{
|
|
|
Name: filename,
|
|
Name: filename,
|
|
|
Url: path,
|
|
Url: path,
|
|
@@ -131,7 +144,7 @@ func InfoTransaction(info *ConnInfo, msg []byte, transactionId string) error {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// InfoTransactionSplit 获取的事务数据内容
|
|
// InfoTransactionSplit 获取的事务数据内容
|
|
|
-func InfoTransactionSplit(info *ConnInfo, msg []byte, transactionId string) error {
|
|
|
|
|
|
|
+func InfoTransactionSplit(info *ConnInfo, msg []byte, transactionId string, transactionIdLen int) error {
|
|
|
fileUser, err := db.FileUserDao{}.Get(transactionId)
|
|
fileUser, err := db.FileUserDao{}.Get(transactionId)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return errors.New("FileUser transactionId is not found")
|
|
return errors.New("FileUser transactionId is not found")
|
|
@@ -139,12 +152,15 @@ func InfoTransactionSplit(info *ConnInfo, msg []byte, transactionId string) erro
|
|
|
if fileUser.UserId == 0 {
|
|
if fileUser.UserId == 0 {
|
|
|
return errors.New("FileUser userId is not found")
|
|
return errors.New("FileUser userId is not found")
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- var splitNum = binary.BigEndian.Uint32(msg[33:37])
|
|
|
|
|
- var pathLength = binary.BigEndian.Uint32(msg[37:41])
|
|
|
|
|
- var path = string(msg[41 : 41+pathLength])
|
|
|
|
|
|
|
+ var index = transactionIdLen + 1
|
|
|
|
|
+ var splitNum = binary.BigEndian.Uint32(msg[index : index+4])
|
|
|
|
|
+ index += 4
|
|
|
|
|
+ var pathLength = binary.BigEndian.Uint32(msg[index : index+4])
|
|
|
|
|
+ index += 4
|
|
|
|
|
+ var path = string(msg[index : index+int(pathLength)])
|
|
|
|
|
+ index += int(pathLength)
|
|
|
path = fmt.Sprint(config.Conf.File.Upload.Path, "/", fileUser.UserId, "/", transactionId, "/", path)
|
|
path = fmt.Sprint(config.Conf.File.Upload.Path, "/", fileUser.UserId, "/", transactionId, "/", path)
|
|
|
- var content = msg[41+pathLength:]
|
|
|
|
|
|
|
+ var content = msg[index:]
|
|
|
err = insertDataAtPosition(path, int64(splitNum*1024), content)
|
|
err = insertDataAtPosition(path, int64(splitNum*1024), content)
|
|
|
return err
|
|
return err
|
|
|
// 返回状态信息
|
|
// 返回状态信息
|