コード例 #1
0
        public async Task<ActionResult> Upload(FilesManagerModel fileModel)
        {
            try
            {
                if(User.IsInRole("User"))
                {
                    if(fileModel.File.ContentLength > 100000000)
                    {
                        return RedirectToAction("Upload");
                    }
                }
                DirectoryInfo directorys = new DirectoryInfo(Server.MapPath(@"..\UserFiles\" + User.Identity.Name));
            
                //Na razie wszystko trafia do roota
                Boolean isRoot = true;
                if (!directorys.Exists)
                {
                    directorys.Create();
                    Folder root = new Folder();
                    root.UserId = User.Identity.GetUserId();
                    root.Name = User.Identity.Name;
                    root.Shared = 0;
                    db.Folders.Add(root);
                    await db.SaveChangesAsync();
                    isRoot = true;
                }

                var fileName = Path.GetFileName(fileModel.File.FileName);
                if (ModelState.IsValid && !db.Files.Any(fi => fi.Name == fileName))
                {
                    if (fileModel != null && fileModel.File != null && fileModel.File.ContentLength > 0)
                    {
                        ESO_Project.Entities.File file = new ESO_Project.Entities.File();
                        

                        using (var fileStream = System.IO.File.Create(Path.Combine(directorys.FullName, fileName)))
                        {
                            fileModel.File.InputStream.Seek(0, SeekOrigin.Begin);
                            fileModel.File.InputStream.CopyTo(fileStream);
                            byte[] fileInBytes = FileEncryption.getBytesFromFile(fileStream);
                        
                            byte[] keyToEncrypt = FileEncryption.getUserKey(User.Identity.GetUserId());
                            byte[] initializationVector = FileEncryption.getInitVector(User.Identity.GetUserId());
                            //byte[] initializationVector = FileEncryption.getInitializationVector();
                            //byte[] keyToEncrypt = FileEncryption.keyGenerator()

                            byte[] encryptedFile = FileEncryption.encrypt(fileInBytes, keyToEncrypt, initializationVector);
                            //keyToEncrypt = FileEncryption.getUserKey(User.Identity.GetUserId());
                            //byte[] decryptedFile = FileEncryption.decrypt(encryptedFile, keyToEncrypt, initializationVector);
                            fileStream.Seek(0,SeekOrigin.Begin);
                            fileStream.Write(encryptedFile, 0, encryptedFile.Length);
                        }
                    
                        //fileModel.File.SaveAs(Path.Combine(directorys.FullName, fileName));
                        if(isRoot)
                        {
                            if(db.Folders.Any(f => f.Name == User.Identity.Name))
                            {
                                    file.FolderId = (from f in db.Folders where f.Name == User.Identity.Name select f.Id).Single();
                                    file.Name = fileName;
                                    file.IsRoot = 0;
                                    file.Type = "." + fileName.Split('.')[1];
                                    file.Size = fileModel.File.ContentLength;
                                    file.Shared = 0;
                                    file.SyncTime = DateTime.Now;
                                    db.Files.Add(file);
                                    await db.SaveChangesAsync();
                                    Logger.log("Dodano plik.");
                            }
                        }
                    
                    }
                    
                }
                return RedirectToAction("Upload");
            }
            catch(Exception e)
            {
                Logger.log(e.StackTrace);
                return View();
            }
        }
コード例 #2
0
 // GET: FilesManager/Delete/5
 public async Task<ActionResult> Delete(int? id)
 {
     try
     {
         ESO_Project.Entities.File file = new ESO_Project.Entities.File();
         file = await db.Files.FindAsync(id);
         if (file == null)
         {
             return RedirectToAction("DatabaseError", "Errors");
         }
         return View(file);
     }
     catch(Exception e)
     {
         Logger.log(e.StackTrace);
         return RedirectToAction("DatabaseError", "Errors");
     }
 }