Example #1
0
        public ActionResult Add(int id)
        {
            var    report      = context.Reports.SingleOrDefault(r => r.Id == id);
            string storageType = ConfigurationManager.AppSettings["StorageType"].ToUpper();

            if (report != null && Request.Files.Count > 0)
            {
                var data = Request.Files.Get(0);

                var extension = GetExtension(data.FileName);

                if (!context.ExtensionWhitelist.Any(x => x.Name == extension))
                {
                    return(Json(new { Error = "Invalid file extension '" + data.FileName + "'." }));
                }

                if (context.Attachments.Any(a => a.Name == data.FileName && a.Report.Id == id))
                {
                    return(Json(new { Error = "Cannot upload file with duplicate name '" + data.FileName + "'." }));
                }

                var attachment = new Attachment {
                    Name = data.FileName
                };
                // Add Conditional for type of file storage
                if (storageType == Constants.StorageType_AWSS3)
                {
                    attachment.StorageId = _s3Storage.UploadFile(data.InputStream);
                }
                else if (storageType == Constants.StorageType_FILE)
                {
                    attachment.StorageId = _storage.Add(data.InputStream);
                }

                report.Attachments.Add(attachment);
                context.SaveChanges();

                return(Json(new { Id = attachment.Id, Name = attachment.Name }));
            }

            return(Json(false));
        }
Example #2
0
        public override bool Save(IHtmlContent htmldoc)
        {
            var docprovider = DocumentStorageProvider.GetInstance(this.DBType, this.ConnectionString);

            CnblogsNewsAdapter adpt = new CnblogsNewsAdapter(htmldoc, this.Url);
            IDocument          doc  = adpt.GetDocument();

            IFileStorage fs = FileStorageProvider.GetInstance(this.DBType, this.ConnectionString);

            foreach (string url in doc.Attachments)
            {
                if (!fs.Exists(url))
                {
                    logger.InfoFormat("Downloading document: {0}", url);
                    var webStream = WebRequestHelper.SendGetRequestAndAutoDetectContentType(url, null, null);
                    fs.Add(url, webStream);
                }
            }
            doc.LastUpdatedDate = DateTime.Now;
            return(docprovider.SaveDocument(doc, true));
        }
        public async Task <MetaInfo> Create(Stream stream, IFileInfo fileInfo)
        {
            if (!securityManager.MayCreate(fileInfo))
            {
                throw new SecurityException("No access to create");
            }

            var meta = new T
            {
                MimeType     = fileInfo.MimeType,
                OriginalName = fileInfo.OriginalName,
                Owner        = fileInfo.Owner,
                Icon         = fileInfo.Icon,
                Extra        = fileInfo.Extra?.ToDictionary(e => e.Key, e => e.Value) // copy values
            };

            meta.Uri = await fileStorage.Add(stream, meta);

            meta.StoragePath = meta.Uri.AbsolutePath;
            metaStorage.Add(meta);
            return(meta);
        }
Example #4
0
        public async Task <IActionResult> Post([FromForm] FileUploadModel model)
        {
            try
            {
                if (model.File?.Length > 0)
                {
                    using var memStream = new MemoryStream();
                    await model.File.CopyToAsync(memStream);

                    await memStream.FlushAsync();

                    return(Created(await _fileStorage.Add(model.File.FileName, memStream), null));
                }
                else
                {
                    return(BadRequest("No files"));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public IActionResult Upload()
        {
            if (!MultipartRequestHelper
                .IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest());
            }

            var defaultFormOptions = new FormOptions();

            var bondary = MultipartRequestHelper
                          .GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType),
                                       defaultFormOptions.MultipartBoundaryLengthLimit);

            var reader = new MultipartReader(bondary, HttpContext.Request.Body);

            var addedFiles = new List <FileModel>();

            foreach (var section in reader.ReadFileContent())
            {
                var fileName = MultipartRequestHelper
                               .ParseFileName(section.ContentDisposition);
                try
                {
                    var addedFile = storage.Add(fileName, section.Body);

                    addedFiles.Add(addedFile);
                }
                catch (ArgumentException)
                {
                    return(BadRequest());
                }
            }

            return(Json(addedFiles));
        }