int FindOrCreatePackageId(SharePointFileLocation packageLocation)
        {
            SPList packagesList = cacheWeb.Lists["Packages"];
            string location     = packageLocation.ToString();

            SPQuery query = new SPQuery();

            query.Query = String.Concat(@"<Where>
                                                <Eq>
                                                    <FieldRef Name='Title'/>
                                                    <Value Type='Text'>", location, @"</Value>
                                                </Eq>
                                            </Where>");
            SPListItemCollection items = packagesList.GetItems(query);

            if (items.Count == 0)
            {
                SPListItem item = packagesList.Items.Add();
                item["Location"] = location;
                item.Update();
                return(item.ID);
            }
            else
            {
                return(items[0].ID);
            }
        }
        public SharePointLibraryCache(SPWeb web, SPFile file, SharePointFileLocation packageLocation, SharePointCacheSettings cacheSettings)
        {
            Uri baseWebUri = new Uri(web.Url);
            Uri cacheUri   = new Uri(baseWebUri, cacheSettings.CachePath);

            cacheSite = new SPSite(cacheUri.ToString());
            cacheWeb  = cacheSite.OpenWeb();
            cacheWeb.AllowUnsafeUpdates = true;

            try
            {
                cacheList = cacheWeb.GetList(cacheSettings.CachePath);
            }
            catch (ArgumentException)
            {
                throw new CacheException(string.Format(CultureInfo.CurrentUICulture, Resources.InvalidLibraryCache, cacheSettings.CachePath));
            }

            int packageId = FindOrCreatePackageId(packageLocation);

            CacheFolderUrl = string.Concat(cacheWeb.Url, "/", cacheList.RootFolder.Url, "/", packageId.ToString(CultureInfo.InvariantCulture));
            cacheFolder    = cacheWeb.GetFolder(CacheFolderUrl);
            if (cacheFolder.Exists == false)
            {
                cacheFolder = CreateCacheFolder(packageId.ToString(CultureInfo.InvariantCulture));
                UnzipAndCachePackage(file);
            }
        }
예제 #3
0
        /// <summary>Creates a package reader for a package without accessing the store.</summary>
        /// <param name="file">The package.</param>
        /// <param name="location">The package location.</param>
        /// <param name="runWithElevatedPrivileges">Whether to run with elevated privileges or not.</param>
        public PackageReader CreatePackageReader(SPFile file, SharePointFileLocation location, bool runWithElevatedPrivileges)
        {
            if (SPContentTypeId.FindCommonParent(file.Item.ContentType.Id, permanentCacheContentType) == permanentCacheContentType)
            {
                object directoryValue = file.Item[new Guid("a76de874-b256-4fd6-8933-813aa8587163")];
                if (directoryValue == null)
                {
                    throw new CacheException(Resources.PermanentCacheNoDirectory);
                }
                else
                {
                    DirectoryInfo directory;
                    try
                    {
                        directory = new DirectoryInfo(directoryValue.ToString());
                    }
                    catch (ArgumentException)
                    {
                        throw new CacheException(Resources.PermanentCacheInvalidDirectory);
                    }
                    catch (PathTooLongException)
                    {
                        throw new CacheException(Resources.PermanentCacheInvalidDirectory);
                    }

                    return(new PermanentCacheSharePointPackageReader(directory, location));
                }
            }
            else
            {
                return(new SharePointPackageReader(CacheSettings, location, file, runWithElevatedPrivileges));
            }
        }
예제 #4
0
        /// <summary>
        /// Creates a package reader to read the specified package from SharePoint. This constructor
        /// optionally causes the file to be read from SharePoint using elevated permissions.
        /// </summary>
        /// <param name="cacheSettings">The settings to use for the caching of this package.
        /// A subdirectory will be created in the cacheSettings.CachePath location with a cached version of this package.</param>
        /// <param name="packageLocation">The location of the package to be read. Any changes to this SharePointFileLocation
        /// object after the SharePointPackageReader is created are not reflected in the behavior of this object.</param>
        /// <param name="runWithElevatedPrivileges">If true, files in SharePoint are accessed using elevated privileges.
        /// If false, the current user credentials are used to access SharePoint files.</param>
        /// <param name="file">The SPFile to read.</param>
        /// <remarks>
        /// <para>
        /// In addition to the exceptions listed below, this method may throw exceptions caused by the
        /// identity not having access to the <paramref name="cacheSettings"/> CachePath location.
        /// </para>
        /// <para>
        /// The contents of the package are not read in the constructor. The contents of the package are read
        /// only once when they are first needed.  If the referenced SharePoint file does not contain a
        /// valid e-learning package, accessing other methods and properties on this object will result
        /// in an <c>InvalidPackageException</c>.
        /// </para>
        /// <para>
        /// If the  <paramref name="cacheSettings"/> CacheInvalidPackageAsFile value is true,
        /// e-learning packages that do not contain basic package information are saved as
        /// files in the cache. In particular, this may increase performance in processing of zip files that do not contain
        /// e-learning content. If false, SharePointPackageReader will not cache zip files that are not e-learning
        /// content. In that case, an application that wants to cache this file would need to cache it as a
        /// CachedSharePointFile. Regardless of the value of this parameter, the SharePointPackageReader will not allow
        /// accessing files from within a package that is not e-learning content.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">Thrown if any argument is null.</exception>
        /// <exception cref="DirectoryNotFoundException">Thrown if the CachePath property of <paramref name="cacheSettings"/>
        /// does not exist prior to calling this constructor.</exception>
        /// <exception cref="FileNotFoundException">Thrown if the requested file does not exist.</exception>
        /// <exception cref="UnauthorizedAccessException">Thrown if the identity doesn't have access to the CachePath provided in the
        /// cache settings.</exception>
        public SharePointPackageReader(SharePointCacheSettings cacheSettings, SharePointFileLocation packageLocation, SPFile file, bool runWithElevatedPrivileges) : base(packageLocation)
        {
            Resources.Culture = Thread.CurrentThread.CurrentCulture;
            Utilities.ValidateParameterNonNull("cacheSettings", cacheSettings);

            RunWithElevatedPrivileges useRequestedPrivileges;

            if (runWithElevatedPrivileges)
            {
                useRequestedPrivileges = SPSecurity.RunWithElevatedPrivileges;
            }
            else
            {
                useRequestedPrivileges = RunWithCurrentUserPrivileges;
            }

            CachedPackage.EnsureCache(cacheSettings);

            CheckFileExists(file, packageLocation);

            // Store variables.
            m_settings = cacheSettings.Clone();

            CachedPackage cachedPackage = null;

            useRequestedPrivileges(delegate
            {
                cachedPackage = new CachedPackage(m_settings, Location, true);
            });

            using (cachedPackage)
            {
                Initialize(new DirectoryInfo(cachedPackage.CacheDir), m_settings.ImpersonationBehavior);
            }
        }
        /// <summary>
        /// Creates a package reader to read the specified package from SharePoint. The package
        /// must be valid e-learning content.The package
        /// is read using the current user's credentials.
        /// </summary>
        /// <remarks>
        /// <para>
        /// In addition to the exceptions listed below, this method may throw exceptions caused by the
        /// identity not having access to the cacheSettings CachePath location.
        /// </para>
        /// <para>
        /// The contents of the package are not read in the constructor.  The contents of the package are read
        /// only once when they are first needed.  If the referenced SharePoint file does not contain a
        /// valid e-learning package, accessing other methods and properties on this object will result
        /// in an <c>InvalidPackageException</c>.
        /// </para>
        /// </remarks>
        /// <param name="cacheDirectory"></param>
        /// <param name="packageLocation">The location of the package to be read. Any changes to this SharePointFileLocation
        /// object after the PermanentCacheSharePointPackageReader is created are not reflected in the behavior of this object.</param>
        public PermanentCacheSharePointPackageReader(DirectoryInfo cacheDirectory, SharePointFileLocation packageLocation) : base(packageLocation)
        {
            Resources.Culture = Thread.CurrentThread.CurrentCulture;
            Utilities.ValidateParameterNonNull("cacheDirectory", cacheDirectory);
            Utilities.ValidateParameterNonNull("packageLocation", packageLocation);

            Initialize(cacheDirectory, ImpersonationBehavior.UseOriginalIdentity);
        }
        /// <summary>
        /// Creates a package reader to read the specified package from SharePoint. This constructor
        /// optionally causes the file to be read from SharePoint using elevated permissions.
        /// </summary>
        /// <param name="cacheSettings">The settings to use for the caching of this package.
        /// A subdirectory will be created in the cacheSettings.CachePath location with a cached version of this package.</param>
        /// <param name="packageLocation">The location of the package to be read. Any changes to this SharePointFileLocation
        /// object after the SharePointLibraryPackageReader is created are not reflected in the behavior of this object.</param>
        /// <param name="file">The file to create the reader for.</param>
        /// <remarks>
        /// <para>
        /// In addition to the exceptions listed below, this method may throw exceptions caused by the
        /// identity not having access to the <paramref name="cacheSettings"/> CachePath location.
        /// </para>
        /// <para>
        /// The contents of the package are not read in the constructor. The contents of the package are read
        /// only once when they are first needed.  If the referenced SharePoint file does not contain a
        /// valid e-learning package, accessing other methods and properties on this object will result
        /// in an <c>InvalidPackageException</c>.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">Thrown if any argument is null.</exception>
        /// <exception cref="FileNotFoundException">Thrown if the requested file does not exist.</exception>
        /// <exception cref="UnauthorizedAccessException">Thrown if the identity doesn't have access to the CachePath provided in the
        /// cache settings.</exception>
        public SharePointLibraryPackageReader(SharePointCacheSettings cacheSettings, SharePointFileLocation packageLocation, SPFile file) : base(packageLocation)
        {
            Resources.Culture = Thread.CurrentThread.CurrentCulture;
            Utilities.ValidateParameterNonNull("cacheSettings", cacheSettings);

            SPWeb web = SPContext.Current.Web;

            cache = new SharePointLibraryCache(web, file, packageLocation, cacheSettings);
        }
 /// <summary>
 /// Internal copy constructor.
 /// </summary>
 /// <param name="copyFrom">Package location to copy from.</param>
 internal SharePointFileLocation(SharePointFileLocation copyFrom)
 {
     Resources.Culture = Thread.CurrentThread.CurrentCulture;
     Utilities.ValidateParameterNonNull("copyFrom", copyFrom);
     SiteId    = copyFrom.SiteId;
     WebId     = copyFrom.WebId;
     FileId    = copyFrom.FileId;
     VersionId = copyFrom.VersionId;
     Timestamp = copyFrom.Timestamp;
 }
        /// <summary>
        /// Gets the directory that the package (specified by packageLocation)
        /// would be cached into.
        /// </summary>
        /// <param name="cachePath">The folder to use for a cache of all SharePoint files.</param>
        /// <param name="impersonationBehavior">The impersonation behaviour to use.</param>
        /// <param name="packageLocation">The package loacation in LearningStore format.</param>
        /// <returns>The directory path to the cached package.</returns>
        internal static string GetCacheDirectory(string cachePath, ImpersonationBehavior impersonationBehavior, string packageLocation)
        {
            SharePointFileLocation location;

            if (!SharePointFileLocation.TryParse(packageLocation, out location))
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.SPFormatInvalid, packageLocation));
            }

            return(SharePointPackageReader.GetCacheDirectory(cachePath, impersonationBehavior, location));
        }
예제 #9
0
        void CheckFileExists(SPFile file, SharePointFileLocation packageLocation)
        {
            if (file.Exists == false)
            {
                throw new FileNotFoundException(Resources.SPFileNotFoundNoName);
            }

            string filename = file.Name;

            // Now check if the version of the file exists.
            if (FileExistsInSharePoint(file, packageLocation.VersionId) == false)
            {
                throw new FileNotFoundException(String.Format(CultureInfo.CurrentCulture, Resources.SPFileNotFound, filename));
            }
        }
        /// <summary>
        /// Constructor. Accesses all SharePoint files with elevated privilege.
        /// </summary>
        /// <param name="packageStore">The PackageStore that contains information about the package.</param>
        /// <param name="packageId">The package id of the package to load.</param>
        /// <param name="packageLocation">The location of the package, as defined in
        /// LearningStore PackageItem.Location column. This cannot be null.</param>
        /// <remarks></remarks>
        internal SharePointPackageStoreReader(SharePointPackageStore packageStore, PackageItemIdentifier packageId, string packageLocation)
        {
            Resources.Culture = Thread.CurrentThread.CurrentCulture;
            Utilities.ValidateParameterNotEmpty("packageLocation", packageLocation);

            m_store     = packageStore;
            m_packageId = packageId;

            SharePointFileLocation location;

            if (!SharePointFileLocation.TryParse(packageLocation, out location))
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.SPFormatInvalid, packageLocation));
            }

            CreatePackageReader(location);
        }
        void CreatePackageReader(SharePointFileLocation location)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                // These methods will throw FileNotFoundException if the package does not exist.

                // If the site does not exist, this throws FileNotFound
                using (SPSite siteElevatedPermissions = new SPSite(location.SiteId))
                {
                    // If the web does not exist, this throws FileNotFound
                    using (SPWeb webElevatedPermissions = siteElevatedPermissions.OpenWeb(location.WebId))
                    {
                        SPFile fileElevatedPermissions = webElevatedPermissions.GetFile(location.FileId);
                        m_spPackageReader = m_store.CreatePackageReader(fileElevatedPermissions, location, true);
                    }
                }
            });
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] // invalid values return false
        public static bool TryParse(string locationValue, out SharePointFileLocation location)
        {
            Utilities.ValidateParameterNotEmpty("locationValue", locationValue);

            location = null;

            // if the location was valid, set all out parameter values and return true
            try
            {
                location = new SharePointFileLocation(locationValue);
            }
            catch
            {
                // if the location was not valid for any reason, return false
                return(false);
            }

            return(true);
        }
예제 #13
0
        /// <summary>
        /// Create a representation of a file in SharePoint, running with elevated privileges when
        /// accessing the SharePoint file.
        /// </summary>
        /// <param name="cacheSettings">The settings that determine how the file is cached.</param>
        /// <param name="location">The location of the file in SharePoint.</param>
        /// <param name="runWithElevatedPrivileges">If true, SharePoint file will be accessed using elevated privileges.</param>
        /// <remarks>
        /// This method does only verifies that the <c>settings.WindowsIdentity</c> has access to the
        /// <c>settings.CachePath</c>. It does not verify that the file exists in SharePoint.
        /// The contents of the package are read only once when they are first needed.
        /// </remarks>
        /// <exception cref="UnauthorizedAccessException">Thrown if the <c>settings.WindowsIdentity</c>
        /// does not have appropriate permissions to the <c>settings.CachePath</c> folder.</exception>
        public CachedSharePointFile(SharePointCacheSettings cacheSettings, SharePointFileLocation location, bool runWithElevatedPrivileges)
        {
            Resources.Culture = Thread.CurrentThread.CurrentCulture;
            Utilities.ValidateParameterNonNull("cacheSettings", cacheSettings);
            Utilities.ValidateParameterNonNull("location", location);

            m_settings = cacheSettings.Clone();
            m_location = location;

            if (runWithElevatedPrivileges)
            {
                m_useRequestedPrivileges = SPSecurity.RunWithElevatedPrivileges;
            }
            else
            {
                m_useRequestedPrivileges = RunWithCurrentUserPrivileges;
            }

            CachedPackage.EnsureCache(cacheSettings);
        }
        private bool m_disposed; // indicates this object has been disposed

        #region constructors
        /// <summary>
        /// Creates a package reader which reads the package from a file system cache.
        /// </summary>
        /// <param name="packageLocation">The location of the package to be read. Any changes to this SharePointFileLocation
        /// object after the FileSystemBasedSharePointPackageReader is created are not reflected in the behavior of this object.</param>
        /// <remarks>
        /// <para>
        /// In addition to the exceptions listed below, this method may throw exceptions caused by the
        /// identity not having access to the CachePath location.
        /// </para>
        /// <para>
        /// The contents of the package are not read in the constructor. The contents of the package are read
        /// only once when they are first needed.  If the referenced SharePoint file does not contain a
        /// valid e-learning package, accessing other methods and properties on this object will result
        /// in an <c>InvalidPackageException</c>.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">Thrown if any argument is null.</exception>
        /// <exception cref="DirectoryNotFoundException">Thrown if the CachePath property
        /// does not exist prior to calling this constructor.</exception>
        /// <exception cref="FileNotFoundException">Thrown if the requested file does not exist.</exception>
        /// <exception cref="UnauthorizedAccessException">Thrown if the identity doesn't have access to the CachePath provided in the
        /// cache settings.</exception>
        public FileSystemBasedSharePointPackageReader(SharePointFileLocation packageLocation) : base(packageLocation)
        {
        }
예제 #15
0
 /// <summary>
 /// Gets the directory that the package (specified by site, web, file, etc)
 /// would be cached into.
 /// </summary>
 /// <param name="cachePath">The folder to use for a cache of all SharePoint files.</param>
 /// <param name="impersonationBehavior">The impersonation behaviour to use.</param>
 /// <param name="packageLocation">The location of the package in SharePoint.</param>
 /// <returns>The directory path to the cached package.</returns>
 internal static string GetCacheDirectory(string cachePath, ImpersonationBehavior impersonationBehavior, SharePointFileLocation packageLocation)
 {
     return(CachedPackage.GetCacheDirectory(cachePath, packageLocation, impersonationBehavior));
 }
예제 #16
0
 /// <summary>Creates a package reader for a package without accessing the store.</summary>
 /// <param name="file">The package.</param>
 /// <param name="location">The package location.</param>
 /// <param name="runWithElevatedPrivileges">Whether to run with elevated privileges or not.</param>
 /// <returns></returns>
 public PackageReader CreatePackageReader(SPFile file, SharePointFileLocation location, bool runWithElevatedPrivileges)
 {
     return(new SharePointLibraryPackageReader(CacheSettings, location, file));
 }
예제 #17
0
 /// <summary>Initializes a new instance of <see cref="SharePointLocationPackageReader"/>.</summary>
 /// <param name="packageLocation">The location of the package.</param>
 public SharePointLocationPackageReader(SharePointFileLocation packageLocation)
 {
     Resources.Culture = Thread.CurrentThread.CurrentCulture;
     Utilities.ValidateParameterNonNull("packageLocation", packageLocation);
     Location = new SharePointFileLocation(packageLocation);
 }
예제 #18
0
        private bool m_disposed;   // indicates this object has been disposed

        /// <summary>
        /// Create a cached copy of a file in SharePoint.
        /// </summary>
        /// <param name="cacheSettings">The settings that determine how the file is cached.</param>
        /// <param name="location">The location of the file in SharePoint.</param>
        /// <remarks>
        /// This method does only verifies that the <c>settings.WindowsIdentity</c> has access to the
        /// <c>settings.CachePath</c>. It does not verify that the file exists in SharePoint.
        /// The contents of the package are read only once when they are first needed.
        /// </remarks>
        public CachedSharePointFile(SharePointCacheSettings cacheSettings, SharePointFileLocation location)
            : this(cacheSettings, location, false)
        {
        }