Ejemplo n.º 1
0
        public int Upload(IFormFile file, int applicationId)
        {
            string savePath = GetSavePath();

            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }

            //string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');

            if (file?.Length > 0 &&
                !string.IsNullOrEmpty(file.FileName))
            {
                //todo create guid and save with guid name
                string fullPath = Path.Combine(savePath, file.FileName);
                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    file.CopyTo(stream);
                }

                if (TryCheckFile(fullPath, out var checkStatus))
                {
                    if (checkStatus.IsOk)
                    {
                        var entity = new ConfModel.Model.File()
                        {
                            Name = file.FileName, ApplicationId = applicationId, Size = file.Length
                        };

                        foreach (var expertId in _fileRepository.GetExpertIds())
                        {
                            entity.FileNotifications.Add(new FileNotification()
                            {
                                UserId = expertId
                            });
                        }

                        return(_fileRepository.Add(entity));
                    }

                    DeleteFile(fullPath);
                    throw new ObjectException(checkStatus);
                }
                else
                {
                    DeleteFile(fullPath);
                    throw new Exception("Exception while checking.");
                }
            }

            throw new Exception("Bad file");
        }
Ejemplo n.º 2
0
 private bool CheckUserPermission(int userId, ConfModel.Model.File file, Application app)
 {
     return(file.ApplicationId == app.Id && app.UserId == userId);
 }