Beispiel #1
0
        public async Task <ContentTypeImportModel> Upload()
        {
            if (Request.Content.IsMimeMultipartContent() == false)
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var root = Current.IOHelper.MapPath(SystemDirectories.TempData.EnsureEndsWith('/') + "FileUploads");

            //ensure it exists
            Directory.CreateDirectory(root);
            var provider = new MultipartFormDataStreamProvider(root);
            var result   = await Request.Content.ReadAsMultipartAsync(provider);

            //must have a file
            if (result.FileData.Count == 0)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            var model = new ContentTypeImportModel();

            var file     = result.FileData[0];
            var fileName = file.Headers.ContentDisposition.FileName.Trim('\"');
            var ext      = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower();

            // renaming the file because MultipartFormDataStreamProvider has created a random fileName instead of using the name from the
            // content-disposition for more than 6 years now. Creating a CustomMultipartDataStreamProvider deriving from MultipartFormDataStreamProvider
            // seems like a cleaner option, but I'm not sure where to put it and renaming only takes one line of code.
            System.IO.File.Move(result.FileData[0].LocalFileName, root + "\\" + fileName);

            if (ext.InvariantEquals("udt"))
            {
                model.TempFileName = Path.Combine(root, fileName);

                model.UploadedFiles.Add(new ContentPropertyFile
                {
                    TempFilePath = model.TempFileName
                });

                var xd = new XmlDocument
                {
                    XmlResolver = null
                };
                xd.Load(model.TempFileName);

                model.Alias = xd.DocumentElement?.SelectSingleNode("//DocumentType/Info/Alias")?.FirstChild.Value;
                model.Name  = xd.DocumentElement?.SelectSingleNode("//DocumentType/Info/Name")?.FirstChild.Value;
            }
            else
            {
                model.Notifications.Add(new Notification(
                                            Services.TextService.Localize("speechBubbles/operationFailedHeader"),
                                            Services.TextService.Localize("media/disallowedFileType"),
                                            NotificationStyle.Warning));
            }

            return(model);
        }
Beispiel #2
0
    public ActionResult <ContentTypeImportModel> Upload(List <IFormFile> file)
    {
        var model = new ContentTypeImportModel();

        foreach (IFormFile formFile in file)
        {
            var fileName = formFile.FileName.Trim(Constants.CharArrays.DoubleQuote);
            var ext      = fileName[(fileName.LastIndexOf('.') + 1)..].ToLower();
        public async Task <ContentTypeImportModel> Upload()
        {
            if (Request.Content.IsMimeMultipartContent() == false)
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var root = IOHelper.MapPath("~/App_Data/TEMP/FileUploads");

            //ensure it exists
            Directory.CreateDirectory(root);
            var provider = new MultipartFormDataStreamProvider(root);
            var result   = await Request.Content.ReadAsMultipartAsync(provider);

            //must have a file
            if (result.FileData.Count == 0)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            var model    = new ContentTypeImportModel();
            var file     = result.FileData[0];
            var fileName = file.Headers.ContentDisposition.FileName.Trim('\"');
            var ext      = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower();

            if (ext.InvariantEquals("udt"))
            {
                //TODO: Currently it has to be here, it's not ideal but that's the way it is right now
                var tempDir = IOHelper.MapPath(SystemDirectories.Data);

                //ensure it's there
                Directory.CreateDirectory(tempDir);

                model.TempFileName = "justDelete_" + Guid.NewGuid() + ".udt";
                var tempFileLocation = Path.Combine(tempDir, model.TempFileName);
                System.IO.File.Copy(file.LocalFileName, tempFileLocation, true);

                var xd = new XmlDocument
                {
                    XmlResolver = null
                };
                xd.Load(tempFileLocation);

                model.Alias = xd.DocumentElement?.SelectSingleNode("//DocumentType/Info/Alias")?.FirstChild.Value;
                model.Name  = xd.DocumentElement?.SelectSingleNode("//DocumentType/Info/Name")?.FirstChild.Value;
            }
            else
            {
                model.Notifications.Add(new Notification(
                                            Services.TextService.Localize("speechBubbles/operationFailedHeader"),
                                            Services.TextService.Localize("media/disallowedFileType"),
                                            SpeechBubbleIcon.Warning));
            }

            return(model);
        }
Beispiel #4
0
        public async Task <ContentTypeImportModel> Upload()
        {
            if (Request.Content.IsMimeMultipartContent() == false)
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var root = IOHelper.MapPath(SystemDirectories.TempData.EnsureEndsWith('/') + "FileUploads");

            //ensure it exists
            Directory.CreateDirectory(root);
            var provider = new MultipartFormDataStreamProvider(root);
            var result   = await Request.Content.ReadAsMultipartAsync(provider);

            //must have a file
            if (result.FileData.Count == 0)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            var model = new ContentTypeImportModel();

            var file     = result.FileData[0];
            var fileName = file.Headers.ContentDisposition.FileName.Trim('\"');
            var ext      = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower();

            if (ext.InvariantEquals("udt"))
            {
                model.TempFileName = Path.Combine(root, model.TempFileName);

                model.UploadedFiles.Add(new ContentPropertyFile
                {
                    TempFilePath = model.TempFileName
                });

                var xd = new XmlDocument
                {
                    XmlResolver = null
                };
                xd.Load(model.TempFileName);

                model.Alias = xd.DocumentElement?.SelectSingleNode("//DocumentType/Info/Alias")?.FirstChild.Value;
                model.Name  = xd.DocumentElement?.SelectSingleNode("//DocumentType/Info/Name")?.FirstChild.Value;
            }
            else
            {
                model.Notifications.Add(new Notification(
                                            Services.TextService.Localize("speechBubbles/operationFailedHeader"),
                                            Services.TextService.Localize("media/disallowedFileType"),
                                            NotificationStyle.Warning));
            }

            return(model);
        }
        public ActionResult <ContentTypeImportModel> Upload(List <IFormFile> file)
        {
            var model = new ContentTypeImportModel();

            foreach (var formFile in file)
            {
                var fileName = formFile.FileName.Trim(Constants.CharArrays.DoubleQuote);
                var ext      = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower();

                var root     = _hostingEnvironment.MapPathContentRoot(Constants.SystemDirectories.TempFileUploads);
                var tempPath = Path.Combine(root, fileName);
                if (Path.GetFullPath(tempPath).StartsWith(Path.GetFullPath(root)))
                {
                    using (var stream = System.IO.File.Create(tempPath))
                    {
                        formFile.CopyToAsync(stream).GetAwaiter().GetResult();
                    }

                    if (ext.InvariantEquals("udt"))
                    {
                        model.TempFileName = Path.Combine(root, fileName);

                        var xd = new XmlDocument
                        {
                            XmlResolver = null
                        };
                        xd.Load(model.TempFileName);

                        model.Alias = xd.DocumentElement?.SelectSingleNode("//DocumentType/Info/Alias")?.FirstChild.Value;
                        model.Name  = xd.DocumentElement?.SelectSingleNode("//DocumentType/Info/Name")?.FirstChild.Value;
                    }
                    else
                    {
                        model.Notifications.Add(new BackOfficeNotification(
                                                    _localizedTextService.Localize("speechBubbles", "operationFailedHeader"),
                                                    _localizedTextService.Localize("media", "disallowedFileType"),
                                                    NotificationStyle.Warning));
                    }
                }
                else
                {
                    model.Notifications.Add(new BackOfficeNotification(
                                                _localizedTextService.Localize("speechBubbles", "operationFailedHeader"),
                                                _localizedTextService.Localize("media", "invalidFileName"),
                                                NotificationStyle.Warning));
                }
            }



            return(model);
        }
        public async Task <ContentTypeImportModel> Upload()
        {
            if (Request.Content.IsMimeMultipartContent() == false)
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var root = IOHelper.MapPath(SystemDirectories.TempData.EnsureEndsWith('/') + "FileUploads");

            //ensure it exists
            Directory.CreateDirectory(root);
            var provider = new MultipartFormDataStreamProvider(root);
            var result   = await Request.Content.ReadAsMultipartAsync(provider);

            //must have a file
            if (result.FileData.Count == 0)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            var model = new ContentTypeImportModel();

            var file     = result.FileData[0];
            var fileName = file.Headers.ContentDisposition.FileName.Trim(Constants.CharArrays.DoubleQuote);
            var ext      = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower();

            var destFileName = Path.Combine(root, fileName);

            if (Path.GetFullPath(destFileName).StartsWith(Path.GetFullPath(root)))
            {
                try
                {
                    // due to a bug before 8.7.0 we didn't delete temp files, so we need to make sure to delete before
                    // moving else you get errors and the upload fails without a message in the UI (there's a JS error)
                    if (System.IO.File.Exists(destFileName))
                    {
                        System.IO.File.Delete(destFileName);
                    }

                    // renaming the file because MultipartFormDataStreamProvider has created a random fileName instead of using the name from the
                    // content-disposition for more than 6 years now. Creating a CustomMultipartDataStreamProvider deriving from MultipartFormDataStreamProvider
                    // seems like a cleaner option, but I'm not sure where to put it and renaming only takes one line of code.
                    System.IO.File.Move(result.FileData[0].LocalFileName, destFileName);
                }
                catch (Exception ex)
                {
                    Logger.Error <ContentTypeController, string>(ex, "Error uploading udt file to App_Data: {File}", destFileName);
                }

                if (ext.InvariantEquals("udt"))
                {
                    model.TempFileName = destFileName;

                    var xd = new XmlDocument
                    {
                        XmlResolver = null
                    };
                    xd.Load(model.TempFileName);

                    model.Alias = xd.DocumentElement?.SelectSingleNode("//DocumentType/Info/Alias")?.FirstChild.Value;
                    model.Name  = xd.DocumentElement?.SelectSingleNode("//DocumentType/Info/Name")?.FirstChild.Value;
                }
                else
                {
                    model.Notifications.Add(new Notification(
                                                Services.TextService.Localize("speechBubbles", "operationFailedHeader"),
                                                Services.TextService.Localize("media", "disallowedFileType"),
                                                NotificationStyle.Warning));
                }
            }
            else
            {
                model.Notifications.Add(new Notification(
                                            Services.TextService.Localize("speechBubbles", "operationFailedHeader"),
                                            Services.TextService.Localize("media", "invalidFileName"),
                                            NotificationStyle.Warning));
            }

            return(model);
        }