示例#1
0
        public void Registration()
        {
            try
            {
                using (var context = new DropBoxContext())
                {
                    if (context.Users.Any(item => item.Key == key || item.Login == NewCommand.Key))
                    {
                        throw new Exception();
                    }

                    User user = new User {
                        Login = NewCommand.Key, Key = key, Password = NewCommand.FileWay
                    };
                    context.Users.Add(user);
                    context.SaveChanges();
                    answer.Add(REGISTRATION + " " + "true");
                    answer.Add("Регистрация прошла успешно");
                    MySocket.Send(ConvertList.ListToByteArray(answer));
                    answer.Clear();
                }
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                answer.Add(REGISTRATION + " " + "false");
                answer.Add("Пльзователь с таким логином или ключом уже присутствует");
                MySocket.Send(ConvertList.ListToByteArray(answer));
                answer.Clear();
            }
        }
示例#2
0
 public Server()
 {
     users   = new Dictionary <long, CommandWork>();
     counter = 0;
     using (var context = new DropBoxContext())
     {
         context.Users.ToList();
     }
 }
示例#3
0
        public void DeleteFile()
        {
            try
            {
                itemNeedToDelete = NewCommand.Key;
                var task = Task.Run(DeleteItem);
                task.Wait();
                answer.Add(DELETE_ITEM);
                answer.Add("Файл удален");

                using (var context = new DropBoxContext())
                {
                    if (context.FilesInfo.ToList().Count != 0)
                    {
                        int      userId = context.Users.ToList().Find(item => item.Key == key).Id;
                        FileInfo info   = context.FilesInfo.ToList().Find(item => item.UserId == userId && item.IsDeleted == false && item.FileWay == itemNeedToDelete);
                        if (info != null)
                        {
                            info.IsDeleted   = true;
                            info.DeletedDate = DateTime.Now;
                            FileInfo oldInfo = context.FilesInfo.ToList().Find(item => item.UserId == userId && item.IsDeleted == false && item.FileWay == itemNeedToDelete);
                            int      index   = context.FilesInfo.ToList().IndexOf(oldInfo);

                            if (index != -1)
                            {
                                context.FilesInfo.ToList()[index] = info;
                            }

                            context.SaveChanges();
                        }
                    }
                }
                MySocket.Send(ConvertList.ListToByteArray(answer));
                answer.Clear();
            }

            catch (AggregateException)
            {
                NewCommand.Key = "false";
                answer.Add(DELETE_ITEM + " false");
                answer.Add("Ошибка при удалении");
                MySocket.Send(ConvertList.ListToByteArray(answer));
                answer.Clear();
            }
        }
示例#4
0
        public void GetLog()
        {
            try
            {
                using (var context = new DropBoxContext())
                {
                    if (context.FilesInfo.ToList().Count != 0)
                    {
                        answer.Add(GET_LOG + " true");

                        foreach (var a in context.FilesInfo.ToList())
                        {
                            if (a.IsDeleted)
                            {
                                answer.Add(a.FileName + '-' + a.CreationDate.ToShortDateString() + " " + a.CreationDate.ToShortTimeString() + '-'
                                           + a.CreationDate.ToShortDateString() + " " + a.DeletedDate.Value.ToShortTimeString());
                            }

                            else
                            {
                                answer.Add(a.FileName + '-' + a.CreationDate.ToShortDateString() + " " + a.CreationDate.ToShortTimeString() + '-' + " ");
                            }
                        }

                        MySocket.Send(ConvertList.ListToByteArray(answer));
                        answer.Clear();
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
            }
            catch (Exception)
            {
                answer.Add(GET_LOG + " " + "false");
                answer.Add(" " + "-" + " " + "-" + " ");
                MySocket.Send(ConvertList.ListToByteArray(answer));
                answer.Clear();
            }
        }
示例#5
0
        public void UploadFile()
        {
            try
            {
                fromFileDownload = NewCommand.Key;
                toFileSave       = NewCommand.FileWay;
                var task = Task.Run(DropBoxUploadFile);
                task.Wait();
                answer.Add(UPLOAD_FILE);
                answer.Add("Файл успешно загружен");

                using (var context = new DropBoxContext())
                {
                    FileInfo info = new FileInfo();
                    info.UserId  = context.Users.ToList().Find(item => item.Key == key).Id;
                    info.FileWay = toFileSave;
                    string[] words = toFileSave.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    info.FileName     = words.Last();
                    info.IsDeleted    = false;
                    info.CreationDate = DateTime.Now;
                    info.DeletedDate  = null;
                    context.FilesInfo.Add(info);
                    context.SaveChanges();
                }

                MySocket.Send(ConvertList.ListToByteArray(answer));
                answer.Clear();
            }
            catch (AggregateException)
            {
                Console.WriteLine("Ошибка при загрузке,возможно вы выбрали не папку а файл для загрузки файла");
                NewCommand.Key = "false";
                answer.Clear();
                answer.Add(UPLOAD_FILE + " false");
                answer.Add("Ошибка при загрузке,возможно вы выбрали не папку для загрузки файла");
                MySocket.Send(ConvertList.ListToByteArray(answer));
                answer.Clear();
            }
        }
示例#6
0
        public void Login()
        {
            try
            {
                bool isLoginSuccess = false;
                using (var context = new DropBoxContext())
                {
                    foreach (var item in context.Users)
                    {
                        if (item.Login == NewCommand.Key && item.Password == NewCommand.FileWay)
                        {
                            isLoginSuccess = true;
                            key            = item.Key;
                        }
                    }
                }

                if (isLoginSuccess)
                {
                    answer.Add(LOGIN + " " + "true");
                    MySocket.Send(ConvertList.ListToByteArray(answer));
                    answer.Clear();
                }

                else
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                answer.Add(LOGIN + " " + "false");
                answer.Add("Вы ввели нправильный логин или пароль");
                MySocket.Send(ConvertList.ListToByteArray(answer));
                answer.Clear();
            }
        }