Beispiel #1
0
        public async Task <IActionResult> ViewVault(string id)
        {
            Models.Vault foundVault = _dbContext.Vaults.Where(v => v.Url == id).FirstOrDefault();
            if (foundVault != null)
            {
                // Update view count
                foundVault.Views += 1;
                _dbContext.Entry(foundVault).State = EntityState.Modified;
                _dbContext.SaveChanges();

                ViewBag.Title = foundVault.Title + " | Vault";

                VaultViewModel model = new VaultViewModel();
                model.CurrentSub = Subdomain;

                model.Url         = foundVault.Url;
                model.UserId      = foundVault.UserId;
                model.User        = foundVault.User;
                model.Title       = foundVault.Title;
                model.Description = foundVault.Description;
                model.DateCreated = foundVault.DateCreated;
                model.DateEdited  = foundVault.DateEdited;

                if (foundVault.VaultItems.Any())
                {
                    foreach (VaultItem item in foundVault.VaultItems.OrderBy(v => v.Index))
                    {
                        if (item.GetType().BaseType == typeof(UploadVaultItem))
                        {
                            UploadVaultItem upload = (UploadVaultItem)item;
                            // Increment Views
                            upload.Upload.Downloads += 1;
                            _dbContext.Entry(upload.Upload).State = EntityState.Modified;
                            _dbContext.SaveChanges();

                            UploadItemViewModel uploadModel = new UploadItemViewModel();
                            uploadModel.VaultItemId = item.VaultItemId;
                            uploadModel.Title       = item.Title;
                            uploadModel.Description = item.Description;
                            uploadModel.DateAdded   = item.DateAdded;
                            uploadModel.Upload      = upload.Upload;
                            model.Items.Add(uploadModel);
                        }
                        else if (item.GetType().BaseType == typeof(PasteVaultItem))
                        {
                            PasteVaultItem paste = (PasteVaultItem)item;
                            // Increment Views
                            paste.Paste.Views += 1;
                            _dbContext.Entry(paste.Paste).State = EntityState.Modified;
                            _dbContext.SaveChanges();

                            // Check Expiration
                            if (PasteHelper.CheckExpiration(paste.Paste))
                            {
                                _dbContext.Pastes.Remove(paste.Paste);
                                _dbContext.SaveChanges();
                                break;
                            }

                            PasteItemViewModel pasteModel = new PasteItemViewModel();
                            pasteModel.VaultItemId = item.VaultItemId;
                            pasteModel.Title       = item.Title;
                            pasteModel.Description = item.Description;
                            pasteModel.DateAdded   = item.DateAdded;

                            pasteModel.PasteId     = paste.Paste.PasteId;
                            pasteModel.Url         = paste.Paste.Url;
                            pasteModel.DatePosted  = paste.Paste.DatePosted;
                            pasteModel.Syntax      = paste.Paste.Syntax;
                            pasteModel.HasPassword = !string.IsNullOrEmpty(paste.Paste.HashedPassword);

                            if (!pasteModel.HasPassword)
                            {
                                // Read in the file
                                string subDir   = paste.Paste.FileName[0].ToString();
                                string filePath = Path.Combine(_config.PasteConfig.PasteDirectory, subDir, paste.Paste.FileName);
                                if (!System.IO.File.Exists(filePath))
                                {
                                    continue;
                                }

                                byte[] ivBytes  = Encoding.Unicode.GetBytes(paste.Paste.IV);
                                byte[] keyBytes = AesCounterManaged.CreateKey(paste.Paste.Key, ivBytes, paste.Paste.KeySize);
                                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                                    using (AesCounterStream cs = new AesCounterStream(fs, false, keyBytes, ivBytes))
                                        using (StreamReader sr = new StreamReader(cs, Encoding.Unicode))
                                        {
                                            pasteModel.Content = await sr.ReadToEndAsync();
                                        }
                            }

                            model.Items.Add(pasteModel);
                        }
                    }
                }

                return(View(model));
            }
            return(new StatusCodeResult(StatusCodes.Status404NotFound));
        }
Beispiel #2
0
        public IActionResult CreateVault(ModifyVaultViewModel model)
        {
            if (model != null)
            {
                if (!string.IsNullOrEmpty(model.title))
                {
                    Models.Vault newVault = new Models.Vault();
                    // Create a new ID
                    string url = StringHelper.RandomString(_config.VaultConfig.UrlLength);
                    while (_dbContext.Vaults.Where(v => v.Url == url).FirstOrDefault() != null)
                    {
                        url = StringHelper.RandomString(_config.VaultConfig.UrlLength);
                    }
                    newVault.Url         = url;
                    newVault.DateCreated = DateTime.Now;
                    newVault.Title       = model.title;
                    newVault.Description = model.description;
                    if (User.Identity.IsAuthenticated)
                    {
                        User user = UserHelper.GetUser(_dbContext, User.Identity.Name);
                        if (user != null)
                        {
                            newVault.UserId = user.UserId;
                        }
                    }

                    // Add/Verify items
                    if (model.items.Any())
                    {
                        int index = 0;
                        foreach (ModifyVaultItemViewModel item in model.items)
                        {
                            if (IsValidItem(item.type, item.url))
                            {
                                switch (item.type.ToLower())
                                {
                                case "upload":
                                    UploadVaultItem newUpload = new UploadVaultItem();
                                    newUpload.Index       = index;
                                    newUpload.DateAdded   = DateTime.Now;
                                    newUpload.Title       = item.title;
                                    newUpload.Description = item.description;
                                    newUpload.UploadId    = _dbContext.Uploads.Where(u => u.Url == item.url).FirstOrDefault().UploadId;
                                    newVault.VaultItems.Add(newUpload);
                                    index++;
                                    break;

                                case "paste":
                                    PasteVaultItem newPaste = new PasteVaultItem();
                                    newPaste.Index       = index;
                                    newPaste.DateAdded   = DateTime.Now;
                                    newPaste.Title       = item.title;
                                    newPaste.Description = item.description;
                                    newPaste.PasteId     = _dbContext.Pastes.Where(p => p.Url == item.url).FirstOrDefault().PasteId;
                                    newVault.VaultItems.Add(newPaste);
                                    index++;
                                    break;

                                default:
                                    return(Json(new { error = new { message = "You have an invalid item type: " + item.type } }));
                                }
                            }
                            else
                            {
                                return(Json(new { error = new { message = "You have an invalid item URL: " + item.url } }));
                            }
                        }
                    }

                    // Add and save the new vault
                    _dbContext.Vaults.Add(newVault);
                    _dbContext.SaveChanges();
                    return(Json(new { result = new { url = Url.SubRouteUrl("v", "Vault.ViewVault", new { id = url }) } }));
                }
                return(Json(new { error = new { message = "You must supply a Title" } }));
            }
            return(Json(new { error = new { message = "Invalid Parameters" } }));
        }