Пример #1
0
        public static ActionResult DownloadFile(BaseController controller, string fileUrl, string mimetype)
        {
            var uri = new Uri(fileUrl);
            var filePath = controller.Server.MapPath(uri.LocalPath);

            if (File.Exists(filePath))
            {
                return controller.FilePublic(filePath, mimetype);
            }

            return new HttpNotFoundResult("File not found");
        }
Пример #2
0
        public static ActionResult DeleteFile(BaseController controller, string fileUrl, IAttachmentLogic attachmentLogic)
        {
            var uri = new Uri(fileUrl);
            var filePath = controller.Server.MapPath(uri.LocalPath);

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            attachmentLogic.RemoveAttachment(new FileInfo(filePath).Name);

            var viewresult = controller.JsonPublic(new { error = string.Empty });
            // for IE8 which does not accept application/json
            if (controller.Request.Headers["Accept"] != null && !controller.Request.Headers["Accept"].Contains("application/json"))
            {
                viewresult.ContentType = "text/plain";
            }

            return viewresult; // trigger success
        }
        public void BaseControllerWebMessagesSetterMustWork()
        {
            BaseController baseController = new BaseController();
            var session = new HttpSessionMock();
            WebMessages webMsg = new WebMessages(session);
            webMsg.AddInfoMessage(RandomData.GetStringSentence(4, true, true));

            baseController.WebMessages = webMsg;

            baseController.WebMessages.Should().BeSameAs(webMsg);
            baseController.WebMessages.Messages.Count.Should().Be(1);
        }
 public void BaseControllerWebMessagesMustBeEmpty()
 {
     BaseController baseController = new BaseController();
     baseController.WebMessages.Should().NotBeNull();
     baseController.WebMessages.Messages.Count.Should().Be(0);
 }
        public void BaseControllerReturnActionIsNotNull()
        {
            // prepare
            var context = HttpMocks.GetControllerContextMock().Object;
            var contr = new BaseController { ControllerContext = context };

            // act
            ReturnControllerActionIdentifier returnValues = contr.ReturnControllerAction;

            // assert
            returnValues.Should().NotBeNull();
        }
Пример #6
0
        /// <summary>
        /// File saving logic to save files to DB and file system
        /// </summary>
        /// <param name="controller">Controller that contains data about uploaded files</param>
        /// <param name="deleteActionUrl">Url that will be used in file deletion</param>
        /// <param name="downloadActionUrl">Url that will be used in file downloading</param>
        /// <param name="attachmentType">Type of attachment block</param>
        /// <param name="attachmentLogic">Logic to have posibility save file information to DB</param>
        /// <param name="applicationId">Main application Id</param>
        /// <param name="attachmentBlock">Attachment block that contains data about all block files</param>
        public static ActionResult UploadFile(BaseController controller, string deleteActionUrl, string downloadActionUrl, AttachmentTypeEnum attachmentType, IAttachmentLogic attachmentLogic, int applicationId, AttachmentBlock attachmentBlock)
        {
            // here we can send in some extra info to be included with the delete url
            var statuses = new List<ViewDataUploadFileResult>();
            for (var i = 0; i < controller.Request.Files.Count; i++)
            {
                var st = StoreFile(new FileSaveModel()
                {
                    File = controller.Request.Files[i],
                    // note how we are adding an additional value to be posted with delete request

                    // and giving it the same value posted with upload
                    DeleteUrl = controller.Url.Action(deleteActionUrl, new { attachmentType }),
                    StorageDirectory = controller.Server.MapPath("~/Content/uploads"),
                    UrlPrefix = ResolveServerUrl(VirtualPathUtility.ToAbsolute("~/Content/uploads"), false), // this is used to generate the relative url of the file

                    // overriding defaults
                    FileName = controller.Request.Files[i].FileName, // default is filename suffixed with filetimestamp
                    ThrowExceptions = true // default is false, if false exception message is set in error property
                });

                statuses.Add(st);
            }

            // statuses contains all the uploaded files details (if error occurs then check error property is not null or empty)
            // todo: add additional code to generate thumbnail for videos, associate files with entities etc

            // adding thumbnail url for jquery file upload javascript plugin
            statuses.ForEach(x => x.ThumbnailUrl = x.Url + string.Empty); // uses ImageResizer httpmodule to resize images from this url

            // setting custom download url instead of direct url to file which is default
            statuses.ForEach(x => x.Url = controller.Url.Action(downloadActionUrl, new { fileUrl = x.Url, mimetype = x.FileType }));

            var viewresult = controller.JsonPublic(new { files = statuses });
            // for IE8 which does not accept application/json
            if (controller.Request.Headers["Accept"] != null && !controller.Request.Headers["Accept"].Contains("application/json"))
            {
                viewresult.ContentType = "text/plain";
            }

            var attachments = new List<Attachment>();
            List<Attachment> newAttachments = null;

            if (attachmentBlock != null && attachmentBlock.Attachments != null)
            {
                newAttachments =
                    attachmentBlock.Attachments.Where(a => string.IsNullOrEmpty(a.ServerFileName)).ToList();
            }

            foreach (var viewDataUploadFileResult in statuses)
            {
                var idx = statuses.IndexOf(viewDataUploadFileResult);

                if (newAttachments != null && newAttachments.Count != 0)
                {
                    viewDataUploadFileResult.Description = newAttachments[idx].Description;
                    viewDataUploadFileResult.DocumentName = newAttachments[idx].DocumentName;
                    newAttachments[idx].ServerFileName = viewDataUploadFileResult.SavedFileName;
                }

                attachments.Add(new Attachment()
                {
                    FileName = viewDataUploadFileResult.Name,
                    ServerFileName = viewDataUploadFileResult.SavedFileName,
                    AttachmentType = attachmentType
                });
            }

            attachmentLogic.AddAttachments(attachmentType, applicationId, attachments, attachmentBlock);

            return viewresult;
        }