private static FileUploadDto UploadFile(
            Stream stream,
            PortalSettings portalSettings,
            UserInfo userInfo,
            string folder,
            string filter,
            string fileName,
            bool overwrite,
            bool isHostPortal,
            bool extract)
        {
            var          result      = new FileUploadDto();
            BinaryReader reader      = null;
            Stream       fileContent = null;

            try
            {
                var extension = Path.GetExtension(fileName).TextOrEmpty().Replace(".", "");
                result.FileIconUrl = IconController.GetFileIconUrl(extension);

                if (!string.IsNullOrEmpty(filter) && !filter.ToLower().Contains(extension.ToLower()))
                {
                    result.Message = GetLocalizedString("ExtensionNotAllowed");
                    return(result);
                }

                if (!IsAllowedExtension(extension))
                {
                    result.Message = GetLocalizedString("ExtensionNotAllowed");
                    return(result);
                }

                var folderManager = FolderManager.Instance;

                var effectivePortalId = isHostPortal ? Null.NullInteger : portalSettings.PortalId;

                // Check if this is a User Folder
                int userId;
                var folderInfo = folderManager.GetFolder(effectivePortalId, folder);
                if (IsUserFolder(folder, out userId))
                {
                    var user = UserController.GetUserById(effectivePortalId, userId);
                    if (user != null)
                    {
                        folderInfo = folderManager.GetUserFolder(user);
                    }
                }

                if (!FolderPermissionController.HasFolderPermission(portalSettings.PortalId, folder, "WRITE") &&
                    !FolderPermissionController.HasFolderPermission(portalSettings.PortalId, folder, "ADD"))
                {
                    result.Message = GetLocalizedString("NoPermission");
                    return(result);
                }

                IFileInfo file;

                // FIX DNN-5917
                fileName = SanitizeFileName(fileName);
                // END FIX

                if (!overwrite && FileManager.Instance.FileExists(folderInfo, fileName, true))
                {
                    result.Message       = GetLocalizedString("AlreadyExists");
                    result.AlreadyExists = true;
                    file          = FileManager.Instance.GetFile(folderInfo, fileName, true);
                    result.FileId = file.FileId;
                }
                else
                {
                    file = FileManager.Instance.AddFile(folderInfo, fileName, stream, true, false,
                                                        FileManager.Instance.GetContentType(Path.GetExtension(fileName)),
                                                        userInfo.UserID);
                    if (extract && extension.ToLower() == "zip")
                    {
                        var destinationFolder = FolderManager.Instance.GetFolder(file.FolderId);
                        var invalidFiles      = new List <string>();
                        var filesCount        = FileManager.Instance.UnzipFile(file, destinationFolder, invalidFiles);

                        var invalidFilesJson = invalidFiles.Count > 0
                            ? string.Format("\"{0}\"", string.Join("\",\"", invalidFiles))
                            : string.Empty;
                        result.Prompt = string.Format("{{\"invalidFiles\":[{0}], \"totalCount\": {1}}}", invalidFilesJson, filesCount);
                    }
                    result.FileId = file.FileId;
                }

                fileContent = FileManager.Instance.GetFileContent(file);

                var path = GetUrl(result.FileId);
                using (reader = new BinaryReader(fileContent))
                {
                    Size size;
                    if (IsImage(fileName))
                    {
                        try
                        {
                            size = ImageHeader.GetDimensions(reader);
                        }
                        catch (ArgumentException exc)
                        {
                            Logger.Warn("Unable to get image dimensions for image file", exc);
                            size = new Size(32, 32);
                        }
                    }
                    else
                    {
                        size = new Size(32, 32);
                    }

                    result.Orientation = size.Orientation();
                }

                result.Path     = result.FileId > 0 ? path : string.Empty;
                result.FileName = fileName;

                if (extract && extension.ToLower() == "zip")
                {
                    FileManager.Instance.DeleteFile(file);
                }

                return(result);
            }
            catch (Exception exe)
            {
                Logger.Error(exe.Message);
                result.Message = exe.Message;
                return(result);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (fileContent != null)
                {
                    fileContent.Close();
                    fileContent.Dispose();
                }
            }
        }
Exemple #2
0
        private static FileUploadDto UploadFile(
            Stream stream,
            int portalId,
            UserInfo userInfo,
            string folder,
            string filter,
            string fileName,
            bool overwrite,
            bool isHostPortal,
            bool extract,
            string validationCode)
        {
            var          result      = new FileUploadDto();
            BinaryReader reader      = null;
            Stream       fileContent = null;

            try
            {
                var extensionList = new List <string>();
                if (!string.IsNullOrWhiteSpace(filter))
                {
                    extensionList = filter.Split(',').Select(i => i.Trim()).ToList();
                }

                var validateParams = new List <object> {
                    extensionList, userInfo.UserID
                };
                if (!userInfo.IsSuperUser)
                {
                    validateParams.Add(portalId);
                }

                if (!ValidationUtils.ValidationCodeMatched(validateParams, validationCode))
                {
                    throw new InvalidOperationException("Bad Request");
                }

                var extension = Path.GetExtension(fileName).ValueOrEmpty().Replace(".", string.Empty);
                result.FileIconUrl = IconController.GetFileIconUrl(extension);

                if (!string.IsNullOrEmpty(filter) && !filter.ToLowerInvariant().Contains(extension.ToLowerInvariant()))
                {
                    result.Message = GetLocalizedString("ExtensionNotAllowed");
                    return(result);
                }

                var folderManager     = FolderManager.Instance;
                var effectivePortalId = isHostPortal ? Null.NullInteger : portalId;
                var folderInfo        = folderManager.GetFolder(effectivePortalId, folder);

                int userId;

                if (folderInfo == null && IsUserFolder(folder, out userId))
                {
                    var user = UserController.GetUserById(effectivePortalId, userId);
                    if (user != null)
                    {
                        folderInfo = folderManager.GetUserFolder(user);
                    }
                }

                if (!FolderPermissionController.HasFolderPermission(portalId, folder, "WRITE") &&
                    !FolderPermissionController.HasFolderPermission(portalId, folder, "ADD"))
                {
                    result.Message = GetLocalizedString("NoPermission");
                    return(result);
                }

                IFileInfo file;

                if (!overwrite && FileManager.Instance.FileExists(folderInfo, fileName, true))
                {
                    result.Message       = GetLocalizedString("AlreadyExists");
                    result.AlreadyExists = true;
                    file          = FileManager.Instance.GetFile(folderInfo, fileName, true);
                    result.FileId = file.FileId;
                }
                else
                {
                    file = FileManager.Instance.AddFile(folderInfo, fileName, stream, true, false,
                                                        FileContentTypeManager.Instance.GetContentType(Path.GetExtension(fileName)),
                                                        userInfo.UserID);
                    if (extract && extension.ToLowerInvariant() == "zip")
                    {
                        var destinationFolder = FolderManager.Instance.GetFolder(file.FolderId);
                        var invalidFiles      = new List <string>();
                        var filesCount        = FileManager.Instance.UnzipFile(file, destinationFolder, invalidFiles);

                        var invalidFilesJson = invalidFiles.Count > 0
                            ? string.Format("\"{0}\"", string.Join("\",\"", invalidFiles))
                            : string.Empty;
                        result.Prompt = string.Format("{{\"invalidFiles\":[{0}], \"totalCount\": {1}}}", invalidFilesJson, filesCount);
                    }

                    result.FileId = file.FileId;
                }

                fileContent = FileManager.Instance.GetFileContent(file);

                var path = GetUrl(result.FileId);
                using (reader = new BinaryReader(fileContent))
                {
                    Size size;
                    if (IsImage(fileName))
                    {
                        try
                        {
                            size = ImageHeader.GetDimensions(reader);
                        }
                        catch (ArgumentException exc)
                        {
                            Logger.Warn("Unable to get image dimensions for image file", exc);
                            size = new Size(32, 32);
                        }
                    }
                    else
                    {
                        size = new Size(32, 32);
                    }

                    result.Orientation = size.Orientation();
                }

                result.Path     = result.FileId > 0 ? path : string.Empty;
                result.FileName = fileName;

                if (extract && extension.ToLowerInvariant() == "zip")
                {
                    FileManager.Instance.DeleteFile(file);
                }

                return(result);
            }
            catch (InvalidFileExtensionException)
            {
                result.Message = GetLocalizedString("ExtensionNotAllowed");
                return(result);
            }
            catch (Exception exe)
            {
                Logger.Error(exe);
                result.Message = exe.Message;
                return(result);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }

                if (fileContent != null)
                {
                    fileContent.Close();
                    fileContent.Dispose();
                }
            }
        }
Exemple #3
0
        private FileUploadViewModel UploadFile(Stream stream, string fileName, bool overwrite, Action <FileUploadViewModel, IFileInfo> onUploadSuccess)
        {
            var result = new FileUploadViewModel();

            try
            {
                var extension = Path.GetExtension(fileName).ValueOrEmpty().Replace(".", "");
                result.FileIconUrl = IconController.GetFileIconUrl(extension);

                if (string.IsNullOrEmpty(extension) || !Host.AllowedExtensionWhitelist.IsAllowedExtension(extension))
                {
                    result.Message = LocalizeString("ExtensionNotAllowed");
                    return(result);
                }

                var folderManager = FolderManager.Instance;
                var fileManager   = FileManager.Instance;

                // Get the user's folder
                var userFolder = folderManager.GetUserFolder(UserInfo);

                string folderPath;
                switch (extension)
                {
                case "ged":
                    folderPath = userFolder.FolderPath + "GEDCOM/";
                    break;

                case "gif":
                case "jpg":
                case "jpeg":
                case "png":
                    folderPath = userFolder.FolderPath + "Images/";
                    break;

                default:
                    folderPath = userFolder.FolderPath + "Documents/";
                    break;
                }

                var folder = folderManager.GetFolders(userFolder).SingleOrDefault(f => f.FolderPath == folderPath) ??
                             folderManager.AddFolder(userFolder.PortalID, folderPath);


                IFileInfo file;

                if (!overwrite && fileManager.FileExists(folder, fileName, true))
                {
                    result.Message       = LocalizeString("AlreadyExists");
                    result.AlreadyExists = true;
                    file          = fileManager.GetFile(folder, fileName, true);
                    result.FileId = file.FileId;
                }
                else
                {
                    file          = fileManager.AddFile(folder, fileName, stream, true, false, "text/plain", UserInfo.UserID);
                    result.FileId = file.FileId;

                    onUploadSuccess(result, file);
                }

                var path = fileManager.GetUrl(file);
                result.Orientation = Orientation.Vertical;

                result.Path     = result.FileId > 0 ? path : string.Empty;
                result.FileName = fileName;

                return(result);
            }
            catch (Exception exe)
            {
                Logger.Error(exe);
                result.Message = exe.Message;
                return(result);
            }
        }
Exemple #4
0
        private static FileUploadDto UploadFile(
            Stream stream,
            PortalSettings portalSettings,
            UserInfo userInfo,
            string folder,
            string filter,
            string fileName,
            bool overwrite,
            bool isHostPortal,
            bool extract)
        {
            var          result      = new FileUploadDto();
            BinaryReader reader      = null;
            Stream       fileContent = null;

            try
            {
                var extension = Path.GetExtension(fileName).TextOrEmpty().Replace(".", "");
                result.FileIconUrl = IconController.GetFileIconUrl(extension);

                if (!string.IsNullOrEmpty(filter) && !filter.ToLower().Contains(extension.ToLower()))
                {
                    result.Message = GetLocalizedString("ExtensionNotAllowed");
                    return(result);
                }

                if (!IsAllowedExtension(extension))
                {
                    result.Message = GetLocalizedString("ExtensionNotAllowed");
                    return(result);
                }

                var folderManager = FolderManager.Instance;

                // Check if this is a User Folder
                var effectivePortalId = isHostPortal
                                            ? Null.NullInteger
                                            : PortalController.GetEffectivePortalId(portalSettings.PortalId);
                int userId;
                var folderInfo = folderManager.GetFolder(effectivePortalId, folder);
                if (IsUserFolder(folder, out userId))
                {
                    var user = UserController.GetUserById(effectivePortalId, userId);
                    if (user != null)
                    {
                        folderInfo = folderManager.GetUserFolder(user);
                    }
                }

                if (!PortalSecurity.IsInRoles(userInfo, portalSettings, folderInfo.FolderPermissions.ToString("WRITE")) &&
                    !PortalSecurity.IsInRoles(userInfo, portalSettings, folderInfo.FolderPermissions.ToString("ADD")))
                {
                    result.Message = GetLocalizedString("NoPermission");
                    return(result);
                }

                IFileInfo file;

                if (!overwrite && FileManager.Instance.FileExists(folderInfo, fileName, true))
                {
                    result.Message       = GetLocalizedString("AlreadyExists");
                    result.AlreadyExists = true;
                    file          = FileManager.Instance.GetFile(folderInfo, fileName, true);
                    result.FileId = file.FileId;
                }
                else
                {
                    file = FileManager.Instance.AddFile(folderInfo, fileName, stream, true, false,
                                                        FileManager.Instance.GetContentType(Path.GetExtension(fileName)),
                                                        userInfo.UserID);
                    if (extract && extension.ToLower() == "zip")
                    {
                        FileManager.Instance.UnzipFile(file);
                    }
                    result.FileId = file.FileId;
                }

                fileContent = FileManager.Instance.GetFileContent(file);

                var path = GetUrl(result.FileId);
                using (reader = new BinaryReader(fileContent))
                {
                    var size = IsImage(fileName) ?
                               ImageHeader.GetDimensions(reader) :
                               new Size(32, 32);

                    result.Orientation = size.Orientation();
                }

                result.Path     = result.FileId > 0 ? path : string.Empty;
                result.FileName = fileName;

                if (extract && extension.ToLower() == "zip")
                {
                    FileManager.Instance.DeleteFile(file);
                }

                return(result);
            }
            catch (Exception exe)
            {
                Logger.Error(exe.Message);
                result.Message = exe.Message;
                return(result);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (fileContent != null)
                {
                    fileContent.Close();
                    fileContent.Dispose();
                }
            }
        }