"io/ioutil"
"math/big"
"os"
+ "path"
"sort"
"strings"
"time"
Handler: HandleSetUser,
},
tranUploadFile: {
- Access: accessUploadFile,
+ Access: accessAlwaysAllow,
DenyMsg: "You are not allowed to upload files.",
Name: "tranUploadFile",
Handler: HandleUploadFile,
}
func HandleGetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
- fileName := string(t.GetField(fieldFileName).Data)
- filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data)
+ fileName := t.GetField(fieldFileName).Data
+ filePath := t.GetField(fieldFilePath).Data
- ffo, err := NewFlattenedFileObject(filePath, fileName)
+ ffo, err := NewFlattenedFileObject(cc.Server.Config.FileRoot, filePath, fileName)
if err != nil {
return res, err
}
res = append(res, cc.NewReply(t,
- NewField(fieldFileName, []byte(fileName)),
+ NewField(fieldFileName, fileName),
NewField(fieldFileTypeString, ffo.FlatFileInformationFork.TypeSignature),
NewField(fieldFileCreatorString, ffo.FlatFileInformationFork.CreatorSignature),
NewField(fieldFileComment, ffo.FlatFileInformationFork.Comment),
// * 210 File comment Optional
// Fields used in the reply: None
func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
- fileName := string(t.GetField(fieldFileName).Data)
- filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data)
+ fileName := t.GetField(fieldFileName).Data
+ filePath := t.GetField(fieldFilePath).Data
+
+ fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
+ if err != nil {
+ return res, err
+ }
+
+ fullNewFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, t.GetField(fieldFileNewName).Data)
+ if err != nil {
+ return nil, err
+ }
+
//fileComment := t.GetField(fieldFileComment).Data
fileNewName := t.GetField(fieldFileNewName).Data
if fileNewName != nil {
- path := filePath + "/" + fileName
- fi, err := os.Stat(path)
+ fi, err := FS.Stat(fullFilePath)
if err != nil {
return res, err
}
}
}
- err = os.Rename(filePath+"/"+fileName, filePath+"/"+string(fileNewName))
+ err = os.Rename(fullFilePath, fullNewFilePath)
if os.IsNotExist(err) {
- res = append(res, cc.NewErrReply(t, "Cannot rename file "+fileName+" because it does not exist or cannot be found."))
+ res = append(res, cc.NewErrReply(t, "Cannot rename file "+string(fileName)+" because it does not exist or cannot be found."))
return res, err
}
}
// * 202 File path
// Fields used in the reply: none
func HandleDeleteFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
- fileName := string(t.GetField(fieldFileName).Data)
- filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data)
+ fileName := t.GetField(fieldFileName).Data
+ filePath := t.GetField(fieldFilePath).Data
- path := filePath + fileName
+ fullFilePath, err := readPath(cc.Server.Config.FileRoot, filePath, fileName)
+ if err != nil {
+ return res, err
+ }
- cc.Server.Logger.Debugw("Delete file", "src", path)
+ cc.Server.Logger.Debugw("Delete file", "src", fullFilePath)
- fi, err := os.Stat(path)
+ fi, err := os.Stat(fullFilePath)
if err != nil {
- res = append(res, cc.NewErrReply(t, "Cannot delete file "+fileName+" because it does not exist or cannot be found."))
+ res = append(res, cc.NewErrReply(t, "Cannot delete file "+string(fileName)+" because it does not exist or cannot be found."))
return res, nil
}
switch mode := fi.Mode(); {
}
}
- if err := os.RemoveAll(path); err != nil {
+ if err := os.RemoveAll(fullFilePath); err != nil {
return res, err
}
// HandleMoveFile moves files or folders. Note: seemingly not documented
func HandleMoveFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
fileName := string(t.GetField(fieldFileName).Data)
- filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data)
- fileNewPath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFileNewPath).Data)
+ filePath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFilePath).Data)
+ fileNewPath := cc.Server.Config.FileRoot + ReadFilePath(t.GetField(fieldFileNewPath).Data)
cc.Server.Logger.Debugw("Move file", "src", filePath+"/"+fileName, "dst", fileNewPath+"/"+fileName)
func HandleNewFolder(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
newFolderPath := cc.Server.Config.FileRoot
+ folderName := string(t.GetField(fieldFileName).Data)
+
+ folderName = path.Join("/", folderName)
// fieldFilePath is only present for nested paths
if t.GetField(fieldFilePath).Data != nil {
var newFp FilePath
- newFp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
+ err := newFp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
+ if err != nil {
+ return nil, err
+ }
newFolderPath += newFp.String()
}
- newFolderPath += "/" + string(t.GetField(fieldFileName).Data)
+ newFolderPath = path.Join(newFolderPath, folderName)
- if err := os.Mkdir(newFolderPath, 0777); err != nil {
- // TODO: Send error response to client
- return []Transaction{}, err
+ // TODO: check path and folder name lengths
+
+ if _, err := FS.Stat(newFolderPath); !os.IsNotExist(err) {
+ msg := fmt.Sprintf("Cannot create folder \"%s\" because there is already a file or folder with that name.", folderName)
+ return []Transaction{cc.NewErrReply(t, msg)}, nil
+ }
+
+ // TODO: check for disallowed characters to maintain compatibility for original client
+
+ if err := FS.Mkdir(newFolderPath, 0777); err != nil {
+ msg := fmt.Sprintf("Cannot create folder \"%s\" because an error occurred.", folderName)
+ return []Transaction{cc.NewErrReply(t, msg)}, nil
}
res = append(res, cc.NewReply(t))
var userFields []Field
// TODO: make order deterministic
for _, acc := range cc.Server.Accounts {
- userField := acc.Payload()
+ userField := acc.MarshalBinary()
userFields = append(userFields, NewField(fieldData, userField))
}
}
func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
- bs := make([]byte, 2)
- binary.BigEndian.PutUint16(bs, *cc.Server.NextGuestID)
-
+ cc.Agreed = true
cc.UserName = t.GetField(fieldUserName).Data
- *cc.ID = bs
*cc.Icon = t.GetField(fieldUserIconID).Data
options := t.GetField(fieldOptions).Data
func HandleDownloadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
fileName := t.GetField(fieldFileName).Data
- filePath := ReadFilePath(t.GetField(fieldFilePath).Data)
+ filePath := t.GetField(fieldFilePath).Data
- ffo, err := NewFlattenedFileObject(cc.Server.Config.FileRoot+filePath, string(fileName))
+ var fp FilePath
+ err = fp.UnmarshalBinary(filePath)
+ if err != nil {
+ return res, err
+ }
+
+ ffo, err := NewFlattenedFileObject(cc.Server.Config.FileRoot, filePath, fileName)
if err != nil {
return res, err
}
transactionRef := cc.Server.NewTransactionRef()
data := binary.BigEndian.Uint32(transactionRef)
- cc.Server.Logger.Infow("File download", "path", filePath)
-
ft := &FileTransfer{
FileName: fileName,
- FilePath: []byte(filePath),
+ FilePath: filePath,
ReferenceNumber: transactionRef,
Type: FileDownload,
}
cc.Transfers[FolderDownload] = append(cc.Transfers[FolderDownload], fileTransfer)
var fp FilePath
- fp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
+ err = fp.UnmarshalBinary(t.GetField(fieldFilePath).Data)
+ if err != nil {
+ return res, err
+ }
+
+ fullFilePath, err := readPath(cc.Server.Config.FileRoot, t.GetField(fieldFilePath).Data, t.GetField(fieldFileName).Data)
- fullFilePath := fmt.Sprintf("%v%v", cc.Server.Config.FileRoot+fp.String(), string(fileTransfer.FileName))
transferSize, err := CalcTotalSize(fullFilePath)
if err != nil {
return res, err
}
func HandleUploadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
+ // TODO: add permission handing for upload folders and drop boxes
+ if !authorize(cc.Account.Access, accessUploadFile) {
+ res = append(res, cc.NewErrReply(t, "You are not allowed to upload files."))
+ return res, err
+ }
+
fileName := t.GetField(fieldFileName).Data
filePath := t.GetField(fieldFilePath).Data
transactionRef := cc.Server.NewTransactionRef()
data := binary.BigEndian.Uint32(transactionRef)
- fileTransfer := &FileTransfer{
+ cc.Server.FileTransfers[data] = &FileTransfer{
FileName: fileName,
FilePath: filePath,
ReferenceNumber: transactionRef,
Type: FileUpload,
}
- cc.Server.FileTransfers[data] = fileTransfer
-
res = append(res, cc.NewReply(t, NewField(fieldRefNum, transactionRef)))
return res, err
}
}
func HandleGetFileNameList(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
- filePath := cc.Server.Config.FileRoot
-
- path := t.GetField(fieldFilePath).Data
- if len(path) > 0 {
- filePath = cc.Server.Config.FileRoot + ReadFilePath(path)
+ fullPath, err := readPath(
+ cc.Server.Config.FileRoot,
+ t.GetField(fieldFilePath).Data,
+ nil,
+ )
+ if err != nil {
+ return res, err
}
- fileNames, err := getFileNameList(filePath)
+ fileNames, err := getFileNameList(fullPath)
if err != nil {
return res, err
}