コード例 #1
0
ファイル: FileLibrary.cs プロジェクト: omeryesil/awapicms
        /// <summary>
        /// Uploads and saves a file
        /// </summary>
        /// <param name="siteId"></param>
        /// <param name="groupIds"></param>
        /// <param name="userId"></param>
        /// <param name="title"></param>
        /// <param name="description"></param>
        /// <param name="fileFullPath"></param>
        /// <param name="fileContent"></param>
        /// <param name="isEnabled"></param>
        /// <param name="size">if the file is an image and the size is set then the image will be resized</param>
        /// <returns></returns>
        public long Upload(long siteId, long[] groupIds, long userId, string title, string description,
                           string fileFullPath, System.IO.Stream fileStream, bool isEnabled, string size)
        {
            //throw error in case of validation problem
            ValidateToUpdateFile(siteId, userId, groupIds);

            //SAVE THE FILE ------------------------------------------------------
            awFile file = new awFile();

            file.fileId = MiscLibrary.CreateUniqueId();

            string     resourceFileName = GetFileNameOnly(fileFullPath);
            string     extension        = GetFileExension(resourceFileName);
            bool       isVideoFile      = _videoLib.isVideoFile(extension);
            int        contentLength    = (int)fileStream.Length;
            string     filePath         = "";
            bool       saveOnLocal      = isVideoFile || !FileAmazonS3.SaveOnAmazonS3();
            SAVED_FILE savedFileInfo    = new SAVED_FILE();

            //SAVE THE FILE FIRST
            try
            {
                file.contentType = GetContentType(AWAPI_BusinessLibrary.library.ConfigurationLibrary.Config.fileMimeXMLPath, extension);

                filePath      = CreateFilePath(saveOnLocal, siteId, file.fileId, extension);
                savedFileInfo = SaveFile(siteId, saveOnLocal, file.contentType, filePath, fileStream, size.Trim());

                file.isOnLocal     = saveOnLocal;
                file.contentSize   = contentLength;
                file.path          = savedFileInfo.filePath.ToLower();
                file.siteId        = siteId;
                file.title         = String.IsNullOrEmpty(title) ? resourceFileName : title;
                file.description   = description;
                file.link          = GetUrl(file.fileId, savedFileInfo.filePath, "").ToLower();
                file.pubDate       = DateTime.Now;
                file.lastBuildDate = DateTime.Now;
                file.isEnabled     = isEnabled;
                file.userId        = userId;
                file.createDate    = DateTime.Now;

                _context.awFiles.InsertOnSubmit(file);
                _context.SubmitChanges();
            }
            catch (Exception ex)
            {
                DeleteFromStorage(siteId, saveOnLocal, filePath);
                throw ex;
            }

            //add file to groups
            AddFileToGroups(file.fileId, groupIds);

            //IF FILE IS A VIDEO FILE THEN CONVERT IT
            if (isVideoFile)
            {
                IfVideoConvertToFLV(file.fileId);
            }

            return(file.fileId);
        }
コード例 #2
0
ファイル: FileLibrary.cs プロジェクト: omeryesil/awapicms
        ///// <summary>
        /////
        ///// </summary>
        ///// <param name="filePath"></param>
        ///// <param name="fileContent"></param>
        //public static SAVED_FILE SaveFile(bool saveToAmazonS3, string contentType, string filePath, System.IO.Stream fileStream)
        //{
        //    //byte[] bytes = StringToByteArray(fileStream);
        //    return SaveFile(saveToAmazonS3, contentType, filePath, bytes);
        //}

        /// <summary>
        /// Saves file to local or Amazon S3
        /// </summary>
        /// <param name="siteId"></param>
        /// <param name="saveToLocal"></param>
        /// <param name="contentType"></param>
        /// <param name="filePath"></param>
        /// <param name="fileStream"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static SAVED_FILE SaveFile(long siteId, bool saveToLocal, string contentType, string filePath, System.IO.Stream fileStream, string size)
        {
            SAVED_FILE file = new SAVED_FILE();

            System.IO.Stream contentStream = new System.IO.MemoryStream();
            filePath      = filePath.ToLower();
            file.fileSize = (int)fileStream.Length;

            try
            {
                Byte[] buffer;

                if (!String.IsNullOrEmpty(size) && contentType.ToLower().IndexOf("image") >= 0)
                {
                    buffer     = AWAPI_Common.library.ImageLibrary.ResizeImage("", fileStream, false, 0, 0, size, false);
                    fileStream = new MemoryStream(buffer);
                }
                if (saveToLocal)
                {
                    int length    = 1024;
                    int bytesRead = 0;
                    buffer = new Byte[length];
                    using (FileStream fs = new FileStream(filePath, FileMode.Create))
                    {
                        do
                        {
                            bytesRead = fileStream.Read(buffer, 0, length);
                            fs.Write(buffer, 0, bytesRead);
                        }while (bytesRead == length);
                    }
                    if (!System.IO.File.Exists(filePath))
                    {
                        throw new Exception("File could not be saved.");
                    }

                    file.filePath = filePath;
                    //fileStream.Dispose();
                }
                else
                {
                    FileAmazonS3 s3 = new FileAmazonS3(siteId);
                    file.filePath = s3.Upload(filePath, fileStream, contentType);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }
            return(file);
        }
コード例 #3
0
ファイル: FileLibrary.cs プロジェクト: omeryesil/awapicms
        /// <summary>
        /// If file is video, then convert it to FLV,
        /// delete the original file, update the database
        /// with new flv's attributes
        /// </summary>
        /// <param name="fileId"></param>
        /// <returns>Returns the file's full path</returns>
        void IfVideoConvertToFLV(long fileId)
        {
            try
            {
                //Get the file from DB
                awFile file = (from l in _context.awFiles
                               where l.fileId.Equals(fileId)
                               select l).FirstOrDefault();
                if (file == null)
                {
                    return;
                }

                string extension = GetFileExension(file.path);
                string path      = file.path;

                //Only Video Files, (flv won't be converted)
                if (!_videoLib.isVideoFile(extension))
                {
                    return;
                }

                //CONVERT and GET THE NEW FILES LOCATION
                OutputPackage videoPackage = _videoLib.ConvertToFLV(path);

                //GET THE NEW FILES INFO
                file.contentSize = videoPackage.Length;
                file.contentType = GetContentType(AWAPI_BusinessLibrary.library.ConfigurationLibrary.Config.fileMimeXMLPath, "flv");

                SAVED_FILE savedFile = new SAVED_FILE();
                if (FileAmazonS3.SaveOnAmazonS3())
                {
                    //load the flv file into byte array, and save it to amazon s3
                    FileStream fileStream = new FileStream(videoPackage.Path, System.IO.FileMode.Open);

                    //SAVE THE VIDEO FILE
                    string keyName = String.Format("{0}/{1}.flv", file.siteId, file.fileId);

                    savedFile = SaveFile(file.siteId, false, file.contentType, keyName, fileStream, "");
                    System.IO.File.Delete(videoPackage.Path);
                    file.path = savedFile.filePath;

                    //SAVE THE THUMB
                    if (videoPackage.PreviewImage != null && !String.IsNullOrEmpty(videoPackage.PreviewImagePath) &&
                        System.IO.File.Exists(videoPackage.PreviewImagePath))
                    {
                        string     thumbFile          = String.Format("{0}/{1}.jpg", file.siteId, file.fileId);
                        FileStream videoPreviewStream = new FileStream(videoPackage.PreviewImagePath, System.IO.FileMode.Open);
                        savedFile      = SaveFile(file.siteId, false, "image/jpeg", thumbFile, videoPreviewStream, "");
                        file.thumbnail = savedFile.filePath;

                        if (System.IO.File.Exists(videoPackage.PreviewImagePath))
                        {
                            System.IO.File.Delete(videoPackage.PreviewImagePath);
                        }
                    }
                    file.isOnLocal = false;
                }
                else
                {
                    file.isOnLocal = true;
                    file.path      = videoPackage.Path;
                    file.thumbnail = videoPackage.PreviewImagePath;
                }

                _context.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }