Exemple #1
0
        /// <summary>
        /// 刪除實體檔案
        /// </summary>
        protected virtual bool DeletePhysicalFileData()
        {
            if (fileFullName == "")
            {
                errState = AttFileErrState.NoInitialize;
                return(false);
            }

            try
            {
                FileInfo fi = new FileInfo(fileFullName);

                if (fi.Exists)
                {
                    fi.Delete();
                }
            }
            catch (Exception ex)
            {
                logger.Error("", ex);
                errState = AttFileErrState.DeletePhysicalFileFailed;

                return(false);
            }

            return(true);
        }
Exemple #2
0
        protected virtual bool CheckExtAndExtractFileInfo(HttpPostedFile postedFile)
        {
            string pathDirFullName  = GetAttRootDirectoryFullName() + filePath;
            string uploadedFileName = Path.GetFileName(postedFile.FileName);
            string ext = Path.GetExtension(postedFile.FileName);

            if (ext.StartsWith("."))
            {
                ext = ext.Substring(1);
            }

            if (!IsFileExtValid(ext))
            {
                errState = AttFileErrState.InvalidFileExt;
                return(false);
            }

            fileSavedName = string.Format("{0:yyyyMMdd_HHmmssfff}.{1}", DateTime.Now, ext);
            fileSize      = postedFile.ContentLength;

            if (fileSize > 0)
            {
                fileSize = (int)Math.Round(fileSize / 1024f, MidpointRounding.AwayFromZero);
            }

            fileMIME     = postedFile.ContentType;
            fileFullName = pathDirFullName + "\\" + fileSavedName;

            return(true);
        }
Exemple #3
0
        public virtual bool DeleteData()
        {
            if (attId == Guid.Empty)
            {
                errState = AttFileErrState.AttIdIsRequired;
                return(false);
            }

            if (fileFullName == "")
            {
                errState = AttFileErrState.NoInitialize;
                return(false);
            }

            ArticlePublisherLogic artPub = new ArticlePublisherLogic(null);
            bool result = artPub.DeleteAttachFileData(attId);

            if (result)
            {
                if (!DeletePhysicalFileData())
                {
                    logger.InfoFormat("can't delete physical file [{0}]", fileFullName);
                }
            }
            else
            {
                errState = AttFileErrState.DeleteDataFailed;
            }

            return(result);
        }
Exemple #4
0
    public static string GetErrMsgOfAttFileErrState(AttFileErrState errState)
    {
        string errMsg = "";

        switch (errState)
        {
        case AttFileErrState.LoadDataFailed:
            errMsg = "載入資料失敗";
            break;

        case AttFileErrState.LoadMultiLangDataFailed:
            errMsg = "載入多語系資料失敗";
            break;

        case AttFileErrState.AttachFileIsRequired:
            errMsg = "請上傳檔案";
            break;

        case AttFileErrState.InvalidFileExt:
            errMsg = "不允許的檔案類型";
            break;

        case AttFileErrState.NoInitialize:
            errMsg = "請先執行初始化";
            break;

        case AttFileErrState.DeleteDataFailed:
            errMsg = "刪除附件失敗";
            break;

        case AttFileErrState.DeletePhysicalFileFailed:
            errMsg = "刪除實體檔案失敗";
            break;

        case AttFileErrState.SavePhysicalFileFailed:
            errMsg = "儲存實體檔案失敗";
            break;

        case AttFileErrState.InsertDataFailed:
            errMsg = "新增附件資料失敗";
            break;

        case AttFileErrState.InsertMultiLangDataFailed:
            errMsg = "新增附件多語系資料失敗";
            break;

        case AttFileErrState.UpdateDataFailed:
            errMsg = "更新附件資料失敗";
            break;

        case AttFileErrState.UpdateMultiLangDataFailed:
            errMsg = "更新附件多語系資料失敗";
            break;

        case AttFileErrState.AttIdIsRequired:
            errMsg = "請提供 AttId";
            break;
        }

        return(errMsg);
    }
Exemple #5
0
    public static string GetErrMsgOfAttFileErrState(AttFileErrState errState)
    {
        string errMsg = "";

        switch (errState)
        {
        case AttFileErrState.LoadDataFailed:
            errMsg = Resources.Lang.ErrMsg_LoadDataFailed;
            break;

        case AttFileErrState.LoadMultiLangDataFailed:
            errMsg = Resources.Lang.ErrMsg_LoadMultiLangDataFailed;
            break;

        case AttFileErrState.AttachFileIsRequired:
            errMsg = Resources.Lang.ErrMsg_AttachFileIsRequired;
            break;

        case AttFileErrState.InvalidFileExt:
            errMsg = Resources.Lang.ErrMsg_InvalidFileExt;
            break;

        case AttFileErrState.NoInitialize:
            errMsg = Resources.Lang.ErrMsg_NoInitialize;
            break;

        case AttFileErrState.DeleteDataFailed:
            errMsg = Resources.Lang.ErrMsg_DeleteAttachmentFailed;
            break;

        case AttFileErrState.DeletePhysicalFileFailed:
            errMsg = Resources.Lang.ErrMsg_DeletePhysicalFileFailed;
            break;

        case AttFileErrState.SavePhysicalFileFailed:
            errMsg = Resources.Lang.ErrMsg_SavePhysicalFileFailed;
            break;

        case AttFileErrState.InsertDataFailed:
            errMsg = Resources.Lang.ErrMsg_InsertAttachmentFailed;
            break;

        case AttFileErrState.InsertMultiLangDataFailed:
            errMsg = Resources.Lang.ErrMsg_InsertMultiLangAttachmentFailed;
            break;

        case AttFileErrState.UpdateDataFailed:
            errMsg = Resources.Lang.ErrMsg_UpdateAttachmentFailed;
            break;

        case AttFileErrState.UpdateMultiLangDataFailed:
            errMsg = Resources.Lang.ErrMsg_UpdateMultiLangAttachmentFailed;
            break;

        case AttFileErrState.AttIdIsRequired:
            errMsg = Resources.Lang.ErrMsg_AttIdIsRequired;
            break;
        }

        return(errMsg);
    }
Exemple #6
0
        /// <summary>
        /// 更新附件資料
        /// </summary>
        protected virtual bool UpdateData(string mdfAccount)
        {
            AttachFileParams param = new AttachFileParams()
            {
                AttId         = attId,
                FilePath      = filePath,
                FileSavedName = fileSavedName,
                FileSize      = fileSize,
                SortNo        = sortNo,
                FileMIME      = fileMIME,
                DontDelete    = dontDelete,
                PostAccount   = postAccount
            };

            ArticlePublisherLogic artPub = new ArticlePublisherLogic(null);
            bool result = artPub.UpdateAttachFileData(param);

            if (result)
            {
                //zh-TW
                if (result && LangManager.IsEnableEditLangZHTW())
                {
                    AttachFileMultiLangParams paramZhTw = new AttachFileMultiLangParams()
                    {
                        AttId        = attId,
                        CultureName  = LangManager.CultureNameZHTW,
                        AttSubject   = attSubjectZhTw,
                        IsShowInLang = isShowInLangZhTw,
                        PostAccount  = postAccount
                    };

                    result = artPub.UpdateAttachFileMultiLangData(paramZhTw);
                }

                //en
                if (result && LangManager.IsEnableEditLangEN())
                {
                    AttachFileMultiLangParams paramEn = new AttachFileMultiLangParams()
                    {
                        AttId        = attId,
                        CultureName  = LangManager.CultureNameEN,
                        AttSubject   = attSubjectEn,
                        IsShowInLang = isShowInLangEn,
                        PostAccount  = postAccount
                    };

                    result = artPub.UpdateAttachFileMultiLangData(paramEn);
                }

                if (!result)
                {
                    errState = AttFileErrState.UpdateMultiLangDataFailed;
                }
            }
            else
            {
                errState = AttFileErrState.UpdateDataFailed;
            }

            return(result);
        }
Exemple #7
0
        /// <summary>
        /// 儲存實體檔案(視條件縮圖)
        /// </summary>
        protected virtual bool SavePhysicalFileDataAndShrinkImage(HttpPostedFile postedFile)
        {
            try
            {
                string      fileExt            = Path.GetExtension(fileSavedName);
                string      contentType        = "application/octet-stream";
                ImageFormat contentImageFormat = ImageFormat.Jpeg;

                switch (fileExt.ToLower())
                {
                case ".jpg":
                case ".jpeg":
                    contentType        = "image/jpeg";
                    contentImageFormat = ImageFormat.Jpeg;
                    break;

                case ".bmp":
                    contentType        = "image/bmp";
                    contentImageFormat = ImageFormat.Bmp;
                    break;

                case ".gif":
                    contentType        = "image/gif";
                    contentImageFormat = ImageFormat.Gif;
                    break;

                case ".png":
                    contentType        = "image/png";
                    contentImageFormat = ImageFormat.Png;
                    break;

                case ".tiff":
                case ".tif":
                    contentType        = "image/tiff";
                    contentImageFormat = ImageFormat.Tiff;
                    break;
                }

                if (contentType == "application/octet-stream")
                {
                    // save normal file
                    postedFile.SaveAs(fileFullName);
                    return(true);
                }

                //載入圖片,檢查是否需要縮圖
                System.Drawing.Image img = System.Drawing.Image.FromStream(postedFile.InputStream);

                int  maxWidth              = 1920;
                int  maxHeight             = 1440;
                bool isHeightMoreThanWidth = false;
                bool needsToShrink         = false;

                if (img.Height > img.Width)
                {
                    isHeightMoreThanWidth = true;
                }

                if (img.Width * img.Height > maxWidth * maxHeight)
                {
                    needsToShrink = true;
                }

                if (needsToShrink)
                {
                    //等比例縮圖
                    float widthRate, heightRate;

                    if (isHeightMoreThanWidth)
                    {
                        widthRate  = img.Width / (float)maxHeight;
                        heightRate = img.Height / (float)maxWidth;
                    }
                    else
                    {
                        widthRate  = img.Width / (float)maxWidth;
                        heightRate = img.Height / (float)maxHeight;
                    }

                    int fitWidth, fitHeight;

                    if (widthRate > heightRate)
                    {
                        fitWidth  = Convert.ToInt32(img.Width / widthRate);
                        fitHeight = Convert.ToInt32(img.Height / widthRate);
                    }
                    else
                    {
                        fitWidth  = Convert.ToInt32(img.Width / heightRate);
                        fitHeight = Convert.ToInt32(img.Height / heightRate);
                    }

                    System.Drawing.Image imgFit = new Bitmap(img, new Size(fitWidth, fitHeight));

                    //還原旋轉方向
                    // reference: https://forums.asp.net/t/2016582.aspx?Resize+script+is+rotating+image+sometimes+
                    if (new List <int>(img.PropertyIdList).Contains(0x112))
                    {
                        PropertyItem prop = img.GetPropertyItem(0x112);

                        if (prop.Type == 3 && prop.Len == 2)
                        {
                            UInt16 orientationExif = BitConverter.ToUInt16(prop.Value, 0);

                            switch (orientationExif)
                            {
                            case 8:
                                imgFit.RotateFlip(RotateFlipType.Rotate270FlipNone);
                                break;

                            case 3:
                                imgFit.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                break;

                            case 6:
                                imgFit.RotateFlip(RotateFlipType.Rotate90FlipNone);
                                break;
                            }
                        }
                    }

                    imgFit.Save(fileFullName, contentImageFormat);
                    imgFit.Dispose();
                }
                else
                {
                    //不用縮圖,直接存檔
                    postedFile.SaveAs(fileFullName);
                }
            }
            catch (Exception ex)
            {
                logger.Error("", ex);
                errState = AttFileErrState.SavePhysicalFileFailed;
                return(false);
            }

            return(true);
        }
Exemple #8
0
        protected virtual bool LoadData()
        {
            bool result = false;

            if (attId != Guid.Empty)
            {
                ArticlePublisherLogic artPub = new ArticlePublisherLogic(null);
                AttachFile            att    = artPub.GetAttachFileDataForBackend(attId);

                if (att == null)
                {
                    errState = AttFileErrState.LoadDataFailed;
                    return(false);
                }

                contextId     = att.ArticleId;
                filePath      = att.FilePath;
                fileSavedName = att.FileSavedName;
                fileSize      = att.FileSize;
                sortNo        = att.SortNo.Value;
                fileMIME      = att.FileMIME;
                dontDelete    = att.DontDelete;
                fileFullName  = string.Format("{0}{1}/{2}", GetAttRootDirectoryFullName(), filePath, fileSavedName);
                postAccount   = att.PostAccount;
                postDate      = att.PostDate.Value;

                if (att.MdfDate.HasValue)
                {
                    mdfAccount = att.MdfAccount;
                    mdfDate    = att.MdfDate.Value;
                }

                //zh-TW
                if (LangManager.IsEnableEditLangZHTW())
                {
                    AttachFileMultiLang attZhTw = artPub.GetAttachFileMultiLangDataForBackend(attId, LangManager.CultureNameZHTW);

                    if (attZhTw == null)
                    {
                        errState = AttFileErrState.LoadMultiLangDataFailed;
                        return(false);
                    }

                    attSubjectZhTw   = attZhTw.AttSubject;
                    isShowInLangZhTw = attZhTw.IsShowInLang;
                    readCountZhTw    = attZhTw.ReadCount;

                    if (attZhTw.MdfDate.HasValue)
                    {
                        DateTime mdfDateZhTw = attZhTw.MdfDate.Value;

                        if (!mdfDate.HasValue || mdfDateZhTw > mdfDate.Value)
                        {
                            mdfAccount = attZhTw.MdfAccount;
                            mdfDate    = mdfDateZhTw;
                        }
                    }
                }

                //en
                if (LangManager.IsEnableEditLangEN())
                {
                    AttachFileMultiLang attEn = artPub.GetAttachFileMultiLangDataForBackend(attId, LangManager.CultureNameEN);

                    if (attEn == null)
                    {
                        errState = AttFileErrState.LoadMultiLangDataFailed;
                        return(false);
                    }

                    attSubjectEn   = attEn.AttSubject;
                    isShowInLangEn = attEn.IsShowInLang;
                    readCountEn    = attEn.ReadCount;

                    if (attEn.MdfDate.HasValue)
                    {
                        DateTime mdfDateEn = attEn.MdfDate.Value;

                        if (!mdfDate.HasValue || mdfDateEn > mdfDate.Value)
                        {
                            mdfAccount = attEn.MdfAccount;
                            mdfDate    = mdfDateEn;
                        }
                    }
                }

                result = true;
            }
            else if (contextId != null)
            {
                // new one
                sortNo = GetNextSortNo();

                result = true;
            }

            return(result);
        }
Exemple #9
0
        public virtual bool SaveData(HttpPostedFile postedFile, string mdfAccount)
        {
            bool hasFile = (postedFile != null && postedFile.ContentLength > 0);

            filePath = GetDirectoryName();
            string        pathDirFullName = GetAttRootDirectoryFullName() + filePath;
            DirectoryInfo diPathDir       = new DirectoryInfo(pathDirFullName);

            if (!diPathDir.Exists)
            {
                diPathDir.Create();
            }

            if (attId != Guid.Empty)
            {
                // update
                if (hasFile)
                {
                    // delete old file
                    if (!DeletePhysicalFileData())
                    {
                        return(false);
                    }

                    if (!CheckExtAndExtractFileInfo(postedFile))
                    {
                        return(false);
                    }

                    // save to disk
                    //儲存實體檔案(視條件縮圖)
                    if (!SavePhysicalFileDataAndShrinkImage(postedFile))
                    {
                        return(false);
                    }
                }

                // save to db
                if (!UpdateData(mdfAccount))
                {
                    return(false);
                }
            }
            else if (contextId.HasValue)
            {
                // add
                if (!hasFile)
                {
                    errState = AttFileErrState.AttachFileIsRequired;
                    return(false);
                }

                if (!CheckExtAndExtractFileInfo(postedFile))
                {
                    return(false);
                }

                // save to disk
                //儲存實體檔案(視條件縮圖)
                if (!SavePhysicalFileDataAndShrinkImage(postedFile))
                {
                    return(false);
                }

                // save to db
                if (!InsertData(mdfAccount))
                {
                    return(false);
                }
            }

            return(true);
        }