Ejemplo n.º 1
0
        private bool CheckOwnerFileValidity(string username, string filename, int incCount)
        {
            //Database functionality
            FileDB.PrimaryKey pk = new FileDB.PrimaryKey(filename, username, incCount);
            if (FileDB.GetFileByKey(pk) == null)//FileDB.GetFileByKey(pk).Equals(null)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        private bool CopyCommand(Socket client, string commandMessage, string username, string filename)
        {
            string ackMessage;
            bool   checkFileValid    = CheckOwnerFileValidity(GetUsernameFromFilename(filename), GetOriginalFileName(filename), GetCopyIdFromFileName(filename));
            string directoryFileName = GetDirectoryFilename(GetUsernameFromFilename(filename), GetOriginalFileName(filename), GetCopyIdFromFileName(filename));
            bool   checkFileExist    = System.IO.File.Exists(directoryFileName);

            if (checkFileValid && checkFileExist && username == GetUsernameFromFilename(filename))
            {
                ackMessage = "ACK " + commandMessage;
                SendClientMessage(client, ackMessage);
                try
                {
                    FileDB.PrimaryKey originalPk = new FileDB.PrimaryKey(GetOriginalFileName(filename), username, GetCopyIdFromFileName(filename));
                    FileDB.PrimaryKey copyPk     = new FileDB.PrimaryKey(GetOriginalFileName(filename), username);
                    File   originalFile          = FileDB.GetFileByKey(originalPk);
                    string originalFileName      = Path.Combine(fileDirectory, originalPk.ToString());
                    string copyFileName          = Path.Combine(fileDirectory, copyPk.ToString());
                    System.IO.File.Copy(originalFileName, copyFileName);
                    FileDB.InsertFile(copyPk, Path.Combine(fileDirectory, copyFileName), originalFile.FileAccessType);


                    string outMessage = "File Copy Finished";
                    logBox.AppendText("Server: " + outMessage + "\n");
                    SendClientMessage(client, outMessage);
                }
                catch (Exception except)
                {
                    logBox.AppendText("Error: during file copy operation.\n");
                    logBox.AppendText("Connection Stopped\n");
                    client.Close();
                    return(false);
                }
            }
            else
            {
                if (!checkFileValid)
                {
                    ackMessage = "ERR " + filename + " does not exist in database!";
                }
                else if (!checkFileExist)
                {
                    ackMessage = "ERR " + filename + " does not exist in directory!";
                }
                else
                {
                    ackMessage = "ERR " + filename + " belonged to someone else!";
                }

                SendClientMessage(client, ackMessage);
                logBox.AppendText("Server: " + ackMessage + "\n");
            }
            return(true);
        }