Exemplo 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);
        }
Exemplo 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);
        }
Exemplo n.º 3
0
        private bool DeleteCommand(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
                {
                    System.IO.File.Delete(directoryFileName);
                    FileDB.PrimaryKey pk = new FileDB.PrimaryKey(GetOriginalFileName(filename), username, GetCopyIdFromFileName(filename));
                    FileDB.DeleteFile(pk);

                    string outMessage = "File Delete Finished";
                    logBox.AppendText("Server: " + outMessage + "\n");
                    SendClientMessage(client, outMessage);
                }
                catch (Exception except)
                {
                    logBox.AppendText("Error: during file delete 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);
        }
Exemplo n.º 4
0
        private bool UploadCommand(Socket client, string commandMessage, string username, string filename)
        {
            bool   fileUploadError = false;
            string ackMessage      = "ACK " + commandMessage;

            SendClientMessage(client, ackMessage);
            //TODO fix naming
            FileDB.PrimaryKey tempPk            = new FileDB.PrimaryKey(filename, username);
            string            directoryFileName = Path.Combine(fileDirectory, tempPk.ToString());
            FileStream        uploadFile        = System.IO.File.Create(directoryFileName);

            Byte[] uploadFileBuffer = new Byte[MAX_BUF];

            Byte[] fileSizeBuffer = new Byte[64];
            try
            {
                int temp = client.Receive(fileSizeBuffer);
            }
            catch
            {
                logBox.AppendText("ERROR: During File Upload\n");
                fileUploadError = true;
                uploadFile.Close();
                return(false);
            }

            ulong fileSize     = BitConverter.ToUInt64(fileSizeBuffer, 0);
            ulong numBytesRead = 0;

            //sendClientMessage(client, "ACK");
            while (fileSize > numBytesRead)
            {
                try
                {
                    int numBytes = client.Receive(uploadFileBuffer);
                    numBytesRead += (ulong)numBytes;

                    int index = Array.FindIndex(uploadFileBuffer, CheckEnd);

                    if (index > -1)
                    {
                        uploadFile.Write(uploadFileBuffer, 0, index);
                        break;
                    }
                    else
                    {
                        uploadFile.Write(uploadFileBuffer, 0, numBytes);
                    }
                }
                catch
                {
                    logBox.AppendText("ERROR: During File Upload\n");
                    uploadFile.Close();
                    fileUploadError = false;
                    break;
                }
            }
            uploadFile.Close();

            if (!fileUploadError)
            {
                FileDB.PrimaryKey newPK = new FileDB.PrimaryKey(filename, username);
                FileDB.InsertFile(newPK, Path.Combine(fileDirectory, (newPK.ToString())));
                logBox.AppendText($"File {newPK.ToString()} UPLOADED\n");
                string message = newPK.IncCount + "." + newPK.FileName + " UPLOADED";
                SendClientMessage(client, message);
            }
            return(true);
        }