public ActionResult AddFile(FormCollection collection) { var temp = collection["hiddenparent"]; int?parentId = int.Parse(temp.ToString()); if (parentId == null) { return(View()); } int projectId = int.Parse(collection["hiddenproject"]); Project proj = projrepository.GetProjectById(projectId); foreach (var fil in folderrepository.GetFolderByID((int)parentId).Files) { if (fil.Name == collection["name"]) { return(RedirectToAction("Index", "Project", new { id = projectId })); } } string extension = GetExtension(collection["Type"].ToString()); Folder par = folderrepository.GetFolderByID((int)parentId); File newFile = new Models.File { Name = collection["name"], Content = "", FolderID = par.ID, Type = collection["Type"], Extension = extension }; filerepository.AddFile(newFile); return(RedirectToAction("Index", "Project", new { id = projectId })); }
public File <int, int> Add(IFolder parent, Stream body, string fileName, bool ensureUniqueName) { var callLog = Log.Call <File <int, int> >($"..., ..., {fileName}, {ensureUniqueName}"); if (ensureUniqueName) { fileName = FindUniqueFileName(parent, fileName); } var fullContentPath = Path.Combine(_oqtServerPaths.FullContentPath(AdamContext.Site.ContentPath), parent.Path); Directory.CreateDirectory(fullContentPath); var filePath = Path.Combine(fullContentPath, fileName); using (var stream = new FileStream(filePath, FileMode.Create)) { body.CopyTo(stream); } FileInfo fileInfo = new FileInfo(filePath); File newAdamFile = new File() { Name = Path.GetFileName(fileName), FolderId = parent.Id, Extension = fileInfo.Extension.ToLowerInvariant().Replace(".", ""), Size = (int)fileInfo.Length, ImageHeight = 0, ImageWidth = 0 }; var dnnFile = FileRepository.AddFile(newAdamFile); return(callLog("ok", GetFile(dnnFile.FileId))); }
public ActionResult UploadFile(string groupid) { string userId = this.User.QID(); if (userId == null) return Unauthorized(); QuantApp.Kernel.User quser = QuantApp.Kernel.User.FindUser(userId); try { foreach(var file in Request.Form.Files) { if (file.Length > 0) { string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); { MemoryStream outStream = new MemoryStream(); file.CopyTo(outStream); byte[] data = outStream.ToArray(); string fid = System.Guid.NewGuid().ToString(); DateTime dt = DateTime.Now; FileRepository.AddFile(fid, fileName, data, file.ContentType, dt, userId, groupid); } } } return Json("Upload Successful."); } catch (System.Exception ex) { return Json("Upload Failed: " + ex.Message); } }
public void SaveFile(string filePath) { //In order to get the data file System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); File f = new File() { Name = fileInfo.Name, Size = fileInfo.Length, CreationDate = fileInfo.CreationTime }; string fileTypeName = fileInfo.Extension.Replace(".", ""); var fileType = _fileTypeRepository.GetFileType(fileTypeName); if (fileType != null) { f.FileTypeId = fileType.FileTypeId; } else { FileType ft = new FileType(); ft.TypeName = fileTypeName; _fileTypeRepository.AddFileType(ft); f.FileTypeId = ft.FileTypeId; } _fileRepository.AddFile(f); }
public ActionResult Create([Bind(Include = "ID,Title,DepartmentID,Description,Contents")] FileViewModel fileVM) { if (ModelState.IsValid) { UserFile file = new UserFile() { Title = fileVM.Title, Description = fileVM.Description, MimeType = fileVM.Contents.ContentType, Department = repo.Department(fileVM.DepartmentID), OriginalFilename = fileVM.Contents.FileName, Content = null }; BinaryReader binaryReader = new BinaryReader(fileVM.Contents.InputStream); file.Content = binaryReader.ReadBytes(fileVM.Contents.ContentLength); repo.AddFile(file); return(RedirectToAction("Index")); } ViewBag.Departments = repo.GetManagedDepartmentsByUserName(User.Identity.Name).Select(d => new SelectListItem() { Text = d.Name, Value = d.ID.ToString() }); return(View(fileVM)); }
public void TestAddFile() { //Arrange var newFile = new File { ID = 6, Content = "var x = 'Hello';", Name = "init.js", Extension = ".js", Type = "javascript" }; //Act _file.AddFile(newFile); //Assert Assert.IsNotNull(_file.GetFileById(newFile.ID)); }
public async Task <IActionResult> UploadFile(List <IFormFile> dummy) { var result = new List <string>(); foreach (var im in Request.Form.Files) { await _repository.AddFile(im.OpenReadStream(), im.FileName); } return(Ok(result)); }
public ActionResult <bool> Add(int ownerId) { FileRepository repository = new FileRepository(context); var file = Request.Form.Files.FirstOrDefault(); //write to file system string path = fileService.WriteToFileSystem(ownerId, file); // add file to the database string size = ((int)(file.Length / 1024f)).ToString() + "kb"; string name = Path.GetFileNameWithoutExtension(path); string extension = Path.GetExtension(path); var response = repository.AddFile(ownerId, size, name, file.ContentType, path, extension); return(response); }
static void BasicRepoOperationsHandler(FileRepository fileRepository, Options options) { foreach (string inputFile in options.InputFiles) { try { fileRepository.AddFile(inputFile); Console.WriteLine("File " + inputFile + " successfully added"); } catch (Exception) { Console.WriteLine("File " + inputFile + " adding error. Skipping..."); } } foreach (string inputFile in options.DeleteFiles) { if (fileRepository.RemoveFile(inputFile)) { Console.WriteLine("Removing file " + inputFile + " successful"); } else { Console.WriteLine("Removing file " + inputFile + " error. File not found"); } } if (options.ExtractFlag) { try { fileRepository.ExtractDataToDir(options.OutDir); Console.WriteLine("Extracting successful"); } catch (Exception) { Console.WriteLine("Extracting error. Operation aborted"); } } }
public Task <bool> AddFile(InMemoryMultipartStreamProviderService provider, string directory, string userId) { return(_fileRepository.AddFile(provider, directory, userId)); }
public ActionResult Create(File f, HttpPostedFileBase file) { FileRepository fr = new FileRepository(); try { if (file != null) { var storage = StorageClient.Create(); string link, filename = ""; string ownerEmail = User.Identity.Name; using (var _f = file.InputStream) { if (f.FileTitle != null) { string rand = Guid.NewGuid().ToString().Substring(0, 4); //stores small part of a guid. filename = f.FileTitle + "_" + rand + System.IO.Path.GetExtension(file.FileName); } else { filename = Guid.NewGuid() + System.IO.Path.GetExtension(file.FileName); } filename = filename.Trim().Replace(" ", "-").Replace(",", "-").Replace("/", "").Replace("\\", "").Replace("&", ""); //Removes any spaces/slashes and & symbol. var storageObject = storage.UploadObject("pfc-file-bucket", filename, null, _f); //Upload file on cloud. link = "https://storage.cloud.google.com/pfc-file-bucket/" + filename; if (null == storageObject.Acl) { storageObject.Acl = new List <ObjectAccessControl>(); } storageObject.Acl.Add(new ObjectAccessControl() { Bucket = "pfc-file-bucket", Entity = $"user-" + ownerEmail, Role = "OWNER", //READER }); var updatedObject = storage.UpdateObject(storageObject, new UpdateObjectOptions() { // Avoid race conditions. IfMetagenerationMatch = storageObject.Metageneration, }); } // Store data in db (Create add method in fileRepo) f.FileOwner = ownerEmail; f.Link = link; f.FileTitle = filename; try { // Adds file to db / Initilize transaction. fr.AddFile(f); } catch (Exception e) { if (fr.MyConnection.State == System.Data.ConnectionState.Closed) { fr.MyConnection.Open(); } ViewBag.Error = "File not created. " + e.Message; fr.MyTransaction.Rollback(); fr.MyConnection.Close(); new LogRepository().LogError(e); } //Cache Update with latest files on DB. CacheRepository cr = new CacheRepository(); cr.UpdateCache(fr.GetFiles(ownerEmail), ownerEmail); //PubSub Add to email Queue //PubSubRepository psr = new PubSubRepository(); //psr.AddToEmailQueue(f); ViewBag.UploadSucc = "File Uploaded Successfully!"; } else { ViewBag.Error = "No File Inputted!"; } } catch (Exception e) { ViewBag.Error = "File not created. " + e.Message; new LogRepository().LogError(e); } // If form is valid clears the entries if (ModelState.IsValid) { ModelState.Clear(); return(View()); } // else entries are not removed so that invalid items can be changed accordingly. else { return(View(f)); } }
public String Store([FromBody] FileContent file) { FileRepository.AddFile(file); return(file.FileId); }
public ActionResult FileUpload(FileUpload sf, HttpPostedFileBase file) { try { if (file != null) { var storage = StorageClient.Create(); string link = ""; using (var f = file.InputStream) { var filename = Guid.NewGuid() + System.IO.Path.GetExtension(file.FileName); var storageObject = storage.UploadObject("justinportellipfc", filename, null, f); link = "https://storage.cloud.google.com/justinportellipfc/" + filename; if (null == storageObject.Acl) { storageObject.Acl = new List <ObjectAccessControl>(); } storageObject.Acl.Add(new ObjectAccessControl() { Bucket = "justinportellipfc", Entity = $"user-" + User.Identity.Name, //whereas [email protected] has to be replaced by a gmail email address who you want to have access granted Role = "OWNER", //READER }); storageObject.Acl.Add(new ObjectAccessControl() { Bucket = "justinportellinpfc", Entity = $"user-" + sf.receiverEmail, //whereas [email protected] has to be replaced by a gmail email address who you want to have access granted Role = "READER", //READER }); var updatedObject = storage.UpdateObject(storageObject, new UpdateObjectOptions() { // Avoid race conditions. IfMetagenerationMatch = storageObject.Metageneration, }); FileRepository fr = new FileRepository(); UsersRepository ur = new UsersRepository(); CacheRepository cr = new CacheRepository(); fr.AddFile(filename.ToString(), link, sf.receiverEmail, ur.GetUserID(User.Identity.Name)); cr.UpdateCache(GetFiles(ur.GetUserID(User.Identity.Name)), ur.GetUserID(User.Identity.Name)); PubSubRepository psr = new PubSubRepository(); psr.AddToEmailQueue(sf); } ViewBag.Message = "File uploaded"; LogsRepository logr = new LogsRepository(); logr.WriteLogEntry("File was Uploaded"); } } catch (Exception ex) { LogsRepository logr = new LogsRepository(); logr.LogError(ex); ViewBag.Message = "File was not uploaded"; } return(View()); }