Пример #1
0
        /// <summary>
        /// metaWeblog.newMediaObject method
        /// </summary>
        /// <param name="blogId">
        /// always 1000 in BlogEngine since it is a singlar blog instance
        /// </param>
        /// <param name="userName">
        /// login username
        /// </param>
        /// <param name="password">
        /// login password
        /// </param>
        /// <param name="mediaObject">
        /// struct with media details
        /// </param>
        /// <param name="request">
        /// The HTTP request.
        /// </param>
        /// <returns>
        /// struct with url to media
        /// </returns>
        internal MWAMediaInfo NewMediaObject(
            string blogId, string userName, string password, MWAMediaObject mediaObject, HttpContext request)
        {
            if (!Security.IsAuthorizedTo(AuthorizationCheck.HasAny, new Rights[]
                {
                    Rights.CreateNewPosts,
                    Rights.CreateNewPages,
                    Rights.EditOwnPosts,
                    Rights.EditOwnPages,
                    Rights.EditOtherUsersPosts
                }))
            {
                throw new MetaWeblogException("11", "User authentication failed");
            }

            var mediaInfo = new MWAMediaInfo();

            var rootPath = string.Format("{0}files/", Blog.CurrentInstance.StorageLocation);
            var serverPath = request.Server.MapPath(rootPath);
            var saveFolder = serverPath;
            string mediaObjectName = mediaObject.name.Replace(" ", "_");
            mediaObjectName = mediaObjectName.Replace(":", "-");
            var fileName = mediaObjectName;
            var mediaFolder = string.Empty;

            // Check/Create Folders & Fix fileName
            if (mediaObjectName.LastIndexOf('/') > -1)
            {
                mediaFolder = mediaObjectName.Substring(0, mediaObjectName.LastIndexOf('/'));
                saveFolder += mediaFolder;
                mediaFolder += "/";
                saveFolder = saveFolder.Replace('/', Path.DirectorySeparatorChar);
                fileName = mediaObjectName.Substring(mediaObjectName.LastIndexOf('/') + 1);
            }
            else
            {
                if (saveFolder.EndsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    saveFolder = saveFolder.Substring(0, saveFolder.Length - 1);
                }
            }

            if (!Directory.Exists(saveFolder))
            {
                Directory.CreateDirectory(saveFolder);
            }

            saveFolder += Path.DirectorySeparatorChar;

            if (File.Exists(saveFolder + fileName))
            {
                // Find unique fileName
                for (var count = 1; count < 30000; count++)
                {
                    var tempFileName = fileName.Insert(fileName.LastIndexOf('.'), string.Format("_{0}", count));
                    if (File.Exists(saveFolder + tempFileName))
                    {
                        continue;
                    }

                    fileName = tempFileName;
                    break;
                }
            }

            // Save File
            using (var fs = new FileStream(saveFolder + fileName, FileMode.Create))
            using (var bw = new BinaryWriter(fs))
            {
                bw.Write(mediaObject.bits);
                bw.Close();
            }

            // Set Url
            var rootUrl = Utils.AbsoluteWebRoot.ToString();
            if (BlogSettings.Instance.RequireSslMetaWeblogApi)
            {
                rootUrl = rootUrl.Replace("https://", "http://");
            }

            var mediaType = mediaObject.type;
            if (mediaType.IndexOf('/') > -1)
            {
                mediaType = mediaType.Substring(0, mediaType.IndexOf('/'));
            }

            switch (mediaType)
            {
                case "image":
                case "notsent":
                    // If there wasn't a type, let's pretend it is an image.  (Thanks Zoundry.  This is for you.)
                    rootUrl += "image.axd?picture=";
                    break;
                default:
                    rootUrl += "file.axd?file=";
                    break;
            }

            mediaInfo.url = rootUrl + mediaFolder + fileName;
            return mediaInfo;
        }
Пример #2
0
        /// <summary>
        /// metaWeblog.newMediaObject
        /// </summary>
        /// <param name="blogID">always 1000 in BlogEngine since it is a singlar blog instance</param>
        /// <param name="userName">login username</param>
        /// <param name="password">login password</param>
        /// <param name="mediaObject">struct with media details</param>
        /// <param name="request">The HTTP request.</param>
        /// <returns>struct with url to media</returns>
        internal MWAMediaInfo NewMediaObject(string blogID, string userName, string password, MWAMediaObject mediaObject, HttpContext request)
        {
            ValidateRequest(userName, password);

            MWAMediaInfo mediaInfo = new MWAMediaInfo();

            string rootPath = BlogSettings.Instance.StorageLocation + "files/";
            string serverPath = request.Server.MapPath(rootPath);
            string saveFolder = serverPath;
            string fileName = mediaObject.name;
            string mediaFolder = "";

            // Check/Create Folders & Fix fileName
            if (mediaObject.name.LastIndexOf('/') > -1)
            {
                mediaFolder = mediaObject.name.Substring(0, mediaObject.name.LastIndexOf('/'));
                saveFolder += mediaFolder;
                mediaFolder += "/";
                saveFolder = saveFolder.Replace('/', Path.DirectorySeparatorChar);
                fileName = mediaObject.name.Substring(mediaObject.name.LastIndexOf('/') + 1);
            }
            else
            {
                if (saveFolder.EndsWith(Path.DirectorySeparatorChar.ToString()))
                    saveFolder = saveFolder.Substring(0, saveFolder.Length - 1);
            }
            if (!Directory.Exists(saveFolder))
                Directory.CreateDirectory(saveFolder);
            saveFolder += Path.DirectorySeparatorChar;

            if (File.Exists(saveFolder + fileName))
            {
                // Find unique fileName
                for (int count = 1; count < 30000; count++)
                {
                    string tempFileName = fileName.Insert(fileName.LastIndexOf('.'), "_" + count);
                    if (!File.Exists(saveFolder + tempFileName))
                    {
                        fileName = tempFileName;
                        break;
                    }
                }
            }

            // Save File
            FileStream fs = new FileStream(saveFolder + fileName, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(mediaObject.bits);
            bw.Close();

            // Set Url
            string rootUrl = Utils.AbsoluteWebRoot.ToString();
            if (BlogSettings.Instance.RequireSSLMetaWeblogAPI)
                rootUrl = rootUrl.Replace("https://", "http://");

            string mediaType = mediaObject.type;
            if (mediaType.IndexOf('/') > -1)
                mediaType = mediaType.Substring(0, mediaType.IndexOf('/'));
            switch (mediaType)
            {
                case "image":
                case "notsent": // If there wasn't a type, let's pretend it is an image.  (Thanks Zoundry.  This is for you.)
                    rootUrl += "image.axd?picture=";
                    break;
                default:
                    rootUrl += "file.axd?file=";
                    break;
            }

            mediaInfo.url = rootUrl + mediaFolder + fileName;
            return mediaInfo;
        }
Пример #3
0
        /// <summary>
        /// metaWeblog.newMediaObject method
        /// </summary>
        /// <param name="blogId">
        /// always 1000 in BlogEngine since it is a singlar blog instance
        /// </param>
        /// <param name="userName">
        /// login username
        /// </param>
        /// <param name="password">
        /// login password
        /// </param>
        /// <param name="mediaObject">
        /// struct with media details
        /// </param>
        /// <param name="request">
        /// The HTTP request.
        /// </param>
        /// <returns>
        /// struct with url to media
        /// </returns>
        internal MWAMediaInfo NewMediaObject(
            string blogId, string userName, string password, MWAMediaObject mediaObject, HttpContext request)
        {
            if (!Security.IsAuthorizedTo(AuthorizationCheck.HasAny, new Rights[]
            {
                Rights.CreateNewPosts,
                Rights.CreateNewPages,
                Rights.EditOwnPosts,
                Rights.EditOwnPages,
                Rights.EditOtherUsersPosts,
                Rights.EditOtherUsersPages
            }))
            {
                throw new MetaWeblogException("11", "User authentication failed");
            }

            var mediaInfo = new MWAMediaInfo();

            var rootPath    = string.Format("{0}files/", BlogSettings.Instance.StorageLocation);
            var serverPath  = request.Server.MapPath(rootPath);
            var saveFolder  = serverPath;
            var fileName    = mediaObject.name;
            var mediaFolder = string.Empty;

            // Check/Create Folders & Fix fileName
            if (mediaObject.name.LastIndexOf('/') > -1)
            {
                mediaFolder  = mediaObject.name.Substring(0, mediaObject.name.LastIndexOf('/'));
                saveFolder  += mediaFolder;
                mediaFolder += "/";
                saveFolder   = saveFolder.Replace('/', Path.DirectorySeparatorChar);
                fileName     = mediaObject.name.Substring(mediaObject.name.LastIndexOf('/') + 1);
            }
            else
            {
                if (saveFolder.EndsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    saveFolder = saveFolder.Substring(0, saveFolder.Length - 1);
                }
            }

            if (!Directory.Exists(saveFolder))
            {
                Directory.CreateDirectory(saveFolder);
            }

            saveFolder += Path.DirectorySeparatorChar;

            if (File.Exists(saveFolder + fileName))
            {
                // Find unique fileName
                for (var count = 1; count < 30000; count++)
                {
                    var tempFileName = fileName.Insert(fileName.LastIndexOf('.'), string.Format("_{0}", count));
                    if (File.Exists(saveFolder + tempFileName))
                    {
                        continue;
                    }

                    fileName = tempFileName;
                    break;
                }
            }

            // Save File
            using (var fs = new FileStream(saveFolder + fileName, FileMode.Create))
                using (var bw = new BinaryWriter(fs))
                {
                    bw.Write(mediaObject.bits);
                    bw.Close();
                }

            // Set Url
            var rootUrl = Utils.AbsoluteWebRoot.ToString();

            if (BlogSettings.Instance.RequireSslMetaWeblogApi)
            {
                rootUrl = rootUrl.Replace("https://", "http://");
            }

            var mediaType = mediaObject.type;

            if (mediaType.IndexOf('/') > -1)
            {
                mediaType = mediaType.Substring(0, mediaType.IndexOf('/'));
            }

            switch (mediaType)
            {
            case "image":
            case "notsent":
                // If there wasn't a type, let's pretend it is an image.  (Thanks Zoundry.  This is for you.)
                rootUrl += "image.axd?picture=";
                break;

            default:
                rootUrl += "file.axd?file=";
                break;
            }

            mediaInfo.url = rootUrl + mediaFolder + fileName;
            return(mediaInfo);
        }
Пример #4
0
        /// <summary>
        /// metaWeblog.newMediaObject
        /// </summary>
        /// <param name="blogID">always 1000 in BlogEngine since it is a singlar blog instance</param>
        /// <param name="userName">login username</param>
        /// <param name="password">login password</param>
        /// <param name="mediaObject">struct with media details</param>
        /// <param name="request">The HTTP request.</param>
        /// <returns>struct with url to media</returns>
        internal MWAMediaInfo NewMediaObject(string blogID, string userName, string password, MWAMediaObject mediaObject, HttpContext request)
        {
            ValidateRequest(userName, password);

            MWAMediaInfo mediaInfo = new MWAMediaInfo();

            string rootPath    = BlogSettings.Instance.StorageLocation + "files/";
            string serverPath  = request.Server.MapPath(rootPath);
            string saveFolder  = serverPath;
            string fileName    = mediaObject.name;
            string mediaFolder = "";

            // Check/Create Folders & Fix fileName
            if (mediaObject.name.LastIndexOf('/') > -1)
            {
                mediaFolder  = mediaObject.name.Substring(0, mediaObject.name.LastIndexOf('/'));
                saveFolder  += mediaFolder;
                mediaFolder += "/";
                saveFolder   = saveFolder.Replace('/', Path.DirectorySeparatorChar);
                fileName     = mediaObject.name.Substring(mediaObject.name.LastIndexOf('/') + 1);
            }
            else
            {
                if (saveFolder.EndsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    saveFolder = saveFolder.Substring(0, saveFolder.Length - 1);
                }
            }
            if (!Directory.Exists(saveFolder))
            {
                Directory.CreateDirectory(saveFolder);
            }
            saveFolder += Path.DirectorySeparatorChar;

            if (File.Exists(saveFolder + fileName))
            {
                // Find unique fileName
                for (int count = 1; count < 30000; count++)
                {
                    string tempFileName = fileName.Insert(fileName.LastIndexOf('.'), "_" + count);
                    if (!File.Exists(saveFolder + tempFileName))
                    {
                        fileName = tempFileName;
                        break;
                    }
                }
            }

            // Save File
            FileStream   fs = new FileStream(saveFolder + fileName, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);

            bw.Write(mediaObject.bits);
            bw.Close();

            // Set Url
            string rootUrl = Utils.AbsoluteWebRoot.ToString();

            if (BlogSettings.Instance.RequireSSLMetaWeblogAPI)
            {
                rootUrl = rootUrl.Replace("https://", "http://");
            }

            string mediaType = mediaObject.type;

            if (mediaType.IndexOf('/') > -1)
            {
                mediaType = mediaType.Substring(0, mediaType.IndexOf('/'));
            }
            switch (mediaType)
            {
            case "image":
            case "notsent":                     // If there wasn't a type, let's pretend it is an image.  (Thanks Zoundry.  This is for you.)
                rootUrl += "image.axd?picture=";
                break;

            default:
                rootUrl += "file.axd?file=";
                break;
            }

            mediaInfo.url = rootUrl + mediaFolder + fileName;
            return(mediaInfo);
        }