public Models.Entities.File UploadFile(HttpPostedFileBase upload)
        {
            Models.Entities.File file = null;
            if (upload != null && upload.ContentLength > 0)
            {
                var filename = Guid.NewGuid().ToString() + Path.GetFileName(upload.FileName);
                //file = new Models.Entities.File
                //{
                //    FileName = Guid.NewGuid().ToString() + System.IO.Path.GetFileName(upload.FileName),
                //    //ContentType = upload.ContentType,
                //};
                //using (var reader = new System.IO.BinaryReader(upload.InputStream))
                //{
                //    file.Content = reader.ReadBytes(upload.ContentLength);
                //}

                string pathForSaving = HostingEnvironment.MapPath("~/Images");
                if (this.CreateFolderIfNeeded(pathForSaving))
                {
                    var uploadFilePathAndName = Path.Combine(pathForSaving, filename);
                    upload.SaveAs(uploadFilePathAndName);
                }
                file = new Models.Entities.File {
                    FileName = filename
                };
            }

            return(file);
        }
Esempio n. 2
0
 public JsonResult GetReadMe(int?ProjectID)
 {
     if (ProjectID.HasValue)
     {
         // We get the project and from that we get the ReadMe file,
         // that we show via model in the Project Index.
         Project Tmp = PService.GetProjectFromID(ProjectID);
         Models.Entities.File ReadMe = Tmp.ReadMe;
         return(Json(ReadMe, JsonRequestBehavior.AllowGet));
     }
     // We use this for JavaScript
     return(Json("", JsonRequestBehavior.AllowGet));
 }
Esempio n. 3
0
 // POST: api/File
 public void Post(string personId, [FromBody] Models.Entities.File file)
 {
     FileTools.AddFileInfoToPerson(repository, personId, file);
 }
Esempio n. 4
0
        public async Task <Result <FileViewModel> > UploadFileAsync(Guid id, IFormFile file, User user)
        {
            if (user == null)
            {
                return(new Result <FileViewModel>(false, null, "Unauthorized", ErrorType.Unauthorized));
            }

            if (file is null)
            {
                return(new Result <FileViewModel>(false, null, "File is not sent", ErrorType.BadRequest));
            }

            var folder = await _context
                         .Folders
                         .Include(x => x.AuthorizedUsers)
                         .Include(x => x.Files)
                         .FirstOrDefaultAsync(x => x.Id == id);

            var canAccess = folder.AuthorizedUsers.Any(x => x.UserId == user.Id && x.AccessType == AccessEnum.Write);

            if (!canAccess && folder.OwnerId != user.Id)
            {
                return(new Result <FileViewModel>(false, null, "Unauthorized", ErrorType.Unauthorized));
            }

            var folderDisk = await _context.Disks.FirstOrDefaultAsync(x => x.Id == folder.DiskHintId);

            if (folderDisk == null || folderDisk.FreeSpace < file.Length)
            {
                return(new Result <FileViewModel>(false, null, "Dostępna przestrzeń dyskowa jest mniejsza niż rozmiar pliku", ErrorType.BadRequest));
            }

            var newFileName = Guid.NewGuid().ToString("N");
            var path        = Path.Combine(_settings.StorageFolderPath, newFileName);

            if (!Directory.Exists(_settings.StorageFolderPath))
            {
                Directory.CreateDirectory(_settings.StorageFolderPath);
            }

            using (var memoryStream = new MemoryStream())
            {
                await file.CopyToAsync(memoryStream);

                var result = await _fileSystem.TrySaveFile(path, memoryStream.ToArray());

                if (!result.Success)
                {
                    return(new Result <FileViewModel>(false, null, result.Error, ErrorType.Internal));
                }
            }

            var newFile = new Models.Entities.File(file.FileName, newFileName, file.Length, user.Id);

            folderDisk.UsedSpace += file.Length;
            folder.Files.Add(newFile);
            await _context.AddAsync(newFile);

            await _context.SaveChangesAsync();

            return(new Result <FileViewModel>(true, new FileViewModel(newFile)));
        }