private async Task SaveImageAsync(User user, string imageUrl)
        {
            using var httpClient = new HttpClient();
            using var response   = await httpClient.GetAsync(imageUrl).ConfigureAwait(false);

            // Determine the image type based on the response header. The url
            // is not enough because in many cases it does not contain an extension.
            // An alternative is to determine the type based on the binary, but that
            // would mean loading the image and converting it in memory using
            // legacy .net APIs.
            var contentTypeHeader = response.Content.Headers.GetValues("Content-Type").FirstOrDefault() ?? string.Empty;
            var contentType       = "png";

            if (contentTypeHeader.Contains("jpg") || contentTypeHeader.Contains("jpeg"))
            {
                contentType = "jpg";
            }
            else if (contentTypeHeader.Contains("gif"))
            {
                contentType = "gif";
            }

            using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

            var bd = UploadHelper.CreateBinaryData("avatar." + contentType, stream);

            user.SetBinary("ImageData", bd);
            user.Save(SavingMode.KeepVersion);
        }
        private void SaveFile()
        {
            // get target container
            var container        = PortalContext.Current.ContextNode as GenericContent;
            var targetFolderName = HttpContext.Current.Request["TargetFolder"];

            if (!string.IsNullOrEmpty(targetFolderName))
            {
                var containerPath = RepositoryPath.Combine(container?.Path, targetFolderName);
                container = Node.LoadNode(containerPath) as GenericContent;

                // create target container if does not exist
                if (container == null)
                {
                    using (new SystemAccount())
                    {
                        container = new Folder(PortalContext.Current.ContextNode)
                        {
                            Name = targetFolderName,
                            AllowedChildTypes = AllowedContentTypesList
                        };
                        container.Save();
                    }
                }
            }

            var postedFile      = Upload.PostedFile;
            var contentTypeName = UploadHelper.GetContentType(postedFile.FileName, container?.Path) ?? typeof(File).Name;

            if (container != null && container.GetAllowedChildTypeNames().Any(ctn => ctn == contentTypeName))
            {
                var binaryData = UploadHelper.CreateBinaryData(postedFile.FileName, postedFile.InputStream);
                var fileName   = binaryData.FileName.ToString();

                var content = ContentRepository.Content.CreateNew(contentTypeName, container, fileName);

                // Uploaded files of different users go to the same folder.
                // Avoid collision, do not overwrite each others' files.
                content.ContentHandler.AllowIncrementalNaming = true;

                content["Name"] = fileName;
                content.Fields["Binary"].SetData(binaryData);
                content.Save();

                // display uploaded file in repeater
                if (_fileListElements == null)
                {
                    _fileListElements = new List <FileListElement>();
                }

                _fileListElements.Add(new FileListElement {
                    FileName = content.Name, Path = content.Path
                });
            }
            else
            {
                ErrorLabel.Text          = "This type cannot be uploaded!";
                ErrorPlaceHolder.Visible = true;
            }
        }
Пример #3
0
        private static void InstallStringResource(string resourceContentName)
        {
            var assembly       = typeof(WebHookComponent).Assembly;
            var resourcePrefix = assembly.GetName().Name;

            // install a string resource stored as an embedded resource
            var          resourcePath = resourcePrefix + ".import.Localization." + resourceContentName;
            const string parentPath   = "/Root/Localization";

            using (var resourceStream = assembly.GetManifestResourceStream(resourcePath))
            {
                var parent = RepositoryTools.CreateStructure(parentPath, "Resources") ??
                             Content.Load(parentPath);
                var whResourcePath = RepositoryPath.Combine(parentPath, resourceContentName);
                var whResource     = Node.Load <Resource>(whResourcePath) ??
                                     new Resource(parent.ContentHandler)
                {
                    Name = resourceContentName
                };
                whResource.Binary = UploadHelper.CreateBinaryData(resourceContentName, resourceStream);
                whResource.Save(SavingMode.KeepVersion);
            }
        }
Пример #4
0
        protected BinaryData CreateBinaryData(IFormFile file, bool setStream = true)
        {
            var fileName = UseChunk ? GetFileName(file) : file?.FileName;

            return(UploadHelper.CreateBinaryData(fileName, setStream ? file?.OpenReadStream() : null, file?.ContentType));
        }
Пример #5
0
        protected BinaryData CreateBinaryData(HttpPostedFile file, bool setStream = true)
        {
            var fileName = UseChunk ? GetFileName(file) : file?.FileName;

            return(UploadHelper.CreateBinaryData(fileName, setStream ? file?.InputStream : null, file?.ContentType));
        }