Esempio n. 1
0
        /// <summary>
        /// Gets the loaded binary depending on storage settings
        /// </summary>
        /// <param name="FileMetaData">FileMetaData</param>
        /// <param name="fromDb">Load from database; otherwise, from file system</param>
        /// <returns>File binary</returns>
        public virtual byte[] LoadBinary(FileMetaData fileData, bool fromDb)
        {
            if (fileData == null)
                throw new ArgumentNullException("fileData");

            byte[] result = null;
            if (fromDb)
                result = fileData.BinaryData;
            else
                result = LoadFromFile(fileData.RowId, fileData.MimeType, (FileType)fileData.FileType);
            return result;
        }
Esempio n. 2
0
        /// <summary>
        /// Delete a file on the physcial system
        /// </summary>
        /// <param name="fileData">FileMetaData</param>
        protected virtual void DeleteOnFileSystem(FileMetaData fileData)
        {
            if (fileData == null)
                throw new ArgumentNullException("fileData");

            string lastPart = GetFileExtensionFromMimeType(fileData.MimeType);
            string localFilename = string.Format("{0}.{1}", fileData.RowId.ToString(), lastPart);
            string localFilepath = Path.Combine(this.LocalPath((FileType)fileData.FileType), localFilename);
            if (File.Exists(localFilepath))
            {
                File.Delete(localFilepath);
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Gets the loaded binary depending on storage settings
 /// </summary>
 /// <param name="FileMetaData">FileMetaData</param>
 /// <returns>File binary</returns>
 public virtual byte[] LoadBinary(FileMetaData fileData)
 {
     return LoadBinary(fileData, this.StoreInDb);
 }
Esempio n. 4
0
        /// <summary>
        /// Inserts a file
        /// </summary>
        /// <param name="dataBinary">The file binary</param>
        /// <param name="mimeType">The file MIME type</param>
        /// <param name="seoFilename">The SEO filename</param>
        /// <param name="isNew">A value indicating whether the file is new</param>
        /// <param name="validateBinary">A value indicating whether to validated provided binary</param>
        /// <returns>Picture</returns>
        public virtual FileMetaData InsertFile(byte[] dataBinary, string mimeType, string seoFilename, bool isNew, FileType fileType)
        {
            mimeType = CommonHelper.EnsureNotNull(mimeType);
            mimeType = CommonHelper.EnsureMaximumLength(mimeType, 20);

            seoFilename = CommonHelper.EnsureMaximumLength(seoFilename, 100);

            var fileData = new FileMetaData()
            {
                BinaryData = this.StoreInDb ? dataBinary : new byte[0],
                MimeType = mimeType,
                SeoFilename = seoFilename,
                FileType = (int)fileType,
                IsNew = isNew,
            };
            fileRepository.Insert(fileData);

            if (!this.StoreInDb)
                SaveInFile(fileData.RowId, dataBinary, mimeType, fileType);

            //event notification
            eventPublisher.EntityInserted(fileData);

            return fileData;
        }
Esempio n. 5
0
 /// <summary>
 /// Get a file local path
 /// </summary>
 /// <param name="FileMetaData">FileMetaData instance</param>
 /// <param name="targetSize">The target picture size (longest side)</param>
 /// <param name="showDefaultPicture">A value indicating whether the default picture is shown</param>
 /// <returns></returns>
 public virtual string GetLocalPath(FileMetaData fileData, bool showDefaultPicture = true)
 {
     string url = GetFileUrl(fileData, showDefaultPicture);
     if (String.IsNullOrEmpty(url))
         return String.Empty;
     else
         return Path.Combine(this.LocalPath((FileType) fileData.FileType ), Path.GetFileName(url));
 }
Esempio n. 6
0
        /// <summary>
        /// Get a file URL
        /// </summary>
        /// <param name="fileData">FileMetaData instance</param>
        /// <param name="targetSize">The target picture size (longest side)</param>
        /// <param name="showDefaultPicture">A value indicating whether the default picture is shown</param>
        /// <returns>URL</returns>
        public virtual string GetFileUrl(FileMetaData fileData, bool showDefaultPicture = true)
        {
            string url = string.Empty;
            byte[] dataBinary = null;
            if (fileData != null)
                dataBinary = LoadBinary(fileData);
            if (fileData == null || dataBinary == null || dataBinary.Length == 0)
            {
                if (showDefaultPicture)
                {
                    url = GetDefaultUrl( (FileType)fileData.FileType );
                }
                return url;
            }

            string lastPart = GetFileExtensionFromMimeType(fileData.MimeType);
            string localFilename;
            if (fileData.IsNew)
            {
                //we do not validate binary here to ensure that no exception ("Parameter is not valid") will be thrown
                fileData = UpdateFile(fileData.RowId,
                    dataBinary,
                    fileData.MimeType,
                    fileData.SeoFilename,
                    false);
            }
            lock (s_lock)
            {
                string seoFileName = fileData.SeoFilename;
                localFilename = !String.IsNullOrEmpty(seoFileName) ?
                        string.Format("{0}_{1}.{2}", fileData.RowId.ToString(), seoFileName, lastPart) :
                        string.Format("{0}.{1}", fileData.RowId.ToString(), lastPart);
            }

            url = "{0}{1}{2}".FormatWith(
                    webHelper.AbsoluteWebRoot.ToString(), this.VirtualPath((FileType)fileData.FileType), localFilename);
            return url;
        }
Esempio n. 7
0
        /// <summary>
        /// Deletes a file
        /// </summary>
        /// <param name="fileData">FileMetaData</param>
        public virtual void DeleteFile(FileMetaData fileData)
        {
            if (fileData == null)
                throw new ArgumentNullException("fileData");

            //delete from file system
            if (!this.StoreInDb)
                DeleteOnFileSystem(fileData);

            //delete from database
            fileRepository.Delete(fileData);

            //event notification
            eventPublisher.EntityDeleted(fileData);
        }