Example #1
0
        public static FileNameMakingMethod GetFileNameMakingMethodFromConfig()
        {
            FileNameMakingMethod method = GetDefaultFileNameMakingMethod();                     // default

            string methodStr = Config.Instance.GetConfig("FileNameMakingMethod");

            if (methodStr == FileNameMakingMethod.PartialUrl.ToString())
            {
                method = FileNameMakingMethod.PartialUrl;
            }
            else if (methodStr == FileNameMakingMethod.FullUrl.ToString())
            {
                method = FileNameMakingMethod.FullUrl;
            }
            else if (methodStr == FileNameMakingMethod.FileName.ToString())
            {
                method = FileNameMakingMethod.FileName;
            }
            else if (methodStr == FileNameMakingMethod.FileNameDuplicateNotAllowed.ToString())
            {
                // use default
            }
            else if (methodStr == FileNameMakingMethod.FileNameOrUrl.ToString())
            {
                method = FileNameMakingMethod.FileNameOrUrl;
            }
            else
            {
                Logger.Warn("Cannot recognize file name making method config value {0}. default value used.", methodStr);
            }

            return(method);
        }
Example #2
0
        public string MakeFilePath(string url, FileNameMakingMethod method)
        {
            string path = null;

            switch (method)
            {
                default:
                    Logger.Warn("File name making method is not valid: {0}", method);

                    goto case FileNameMakingMethod.FileNameOrUrl;

                case FileNameMakingMethod.PartialUrl:
                    {
                        // 마지막 디렉토리 + 파일이름
                        // ex) www.hello.com/test.jpg
                        // ex) www.my.com/1/test.jpg

                        // find last /
                        int last = url.LastIndexOf('/');
                        int secondlast = url.Substring(0, last).LastIndexOf('/');

                        if(secondlast == -1)
                        {
                            secondlast = last;
                        }

                        string partialurl = url.Substring(secondlast + 1);
                        string filename = HttpUtility.UrlEncode(partialurl);

                        Logger.DLog("partial url: {0}, filename: {1} from full url: {2}", partialurl, filename, url);

                        path = GetFilePath(filename);
                    }
                    break;

                case FileNameMakingMethod.FullUrl:
                    {
                        string filename = HttpUtility.UrlEncode(url);

                        path = GetFilePath(filename);
                    }
                    break;

                case FileNameMakingMethod.FileName:
                    {
                        string last = url.Substring(url.LastIndexOf('/') + 1);
                        string filename = HttpUtility.UrlEncode(last);

                        path = GetFilePath(filename);
                    }
                    break;

                case FileNameMakingMethod.FileNameDuplicateNotAllowed:
                    /*
                    {
                        string last = url.Substring(url.LastIndexOf('/') + 1);
                        string filename = HttpUtility.UrlEncode(last);

                        // 중복 파일 검사
                        path = GetFilePath(filename);
                        string alterPath = null;

                        if (File.Exists(path))
                        {
                            Logger.Info("File path {0} already exists. try alter path.", path);

                            int count = 1;

                            do
                            {
                                // make alter path
                                string nameonly;
                                string ext;

                                ExtractFileNameAndExt(filename, out nameonly, out ext);

                                alterPath = string.Format("{0}({1}){2}", nameonly, count++, ext);
                                alterPath = GetFilePath(alterPath);
                            }
                            while (File.Exists(alterPath));

                            path = alterPath;
                        }
                    }
                    break;
                     */
                    // 사라진 옵션이니 FileNameOrUrl로 대체

                case FileNameMakingMethod.FileNameOrUrl:
                    {
                        // by filename first
                        path = MakeFilePath(url, FileNameMakingMethod.FileName);

                        // check dup
                        if (File.Exists(path))
                        {
                            Logger.DLog("file already exists, try url");

                            // by url
                            path = MakeFilePath(url, FileNameMakingMethod.FullUrl);
                        }
                    }
                    break;
            }

            return path;
        }
Example #3
0
        public string MakeFilePath(string url, FileNameMakingMethod method)
        {
            string path = null;

            switch (method)
            {
            default:
                Logger.Warn("File name making method is not valid: {0}", method);

                goto case FileNameMakingMethod.FileNameOrUrl;

            case FileNameMakingMethod.PartialUrl:
            {
                // 마지막 디렉토리 + 파일이름
                // ex) www.hello.com/test.jpg
                // ex) www.my.com/1/test.jpg

                // find last /
                int last       = url.LastIndexOf('/');
                int secondlast = url.Substring(0, last).LastIndexOf('/');

                if (secondlast == -1)
                {
                    secondlast = last;
                }

                string partialurl = url.Substring(secondlast + 1);
                string filename   = HttpUtility.UrlEncode(partialurl);

                Logger.DLog("partial url: {0}, filename: {1} from full url: {2}", partialurl, filename, url);

                path = GetFilePath(filename);
            }
            break;

            case FileNameMakingMethod.FullUrl:
            {
                string filename = HttpUtility.UrlEncode(url);

                path = GetFilePath(filename);
            }
            break;

            case FileNameMakingMethod.FileName:
            {
                string last     = url.Substring(url.LastIndexOf('/') + 1);
                string filename = HttpUtility.UrlEncode(last);

                path = GetFilePath(filename);
            }
            break;

            case FileNameMakingMethod.FileNameDuplicateNotAllowed:
            /*
             * {
             *      string last = url.Substring(url.LastIndexOf('/') + 1);
             *      string filename = HttpUtility.UrlEncode(last);
             *
             *      // 중복 파일 검사
             *      path = GetFilePath(filename);
             *      string alterPath = null;
             *
             *      if (File.Exists(path))
             *      {
             *              Logger.Info("File path {0} already exists. try alter path.", path);
             *
             *              int count = 1;
             *
             *              do
             *              {
             *                      // make alter path
             *                      string nameonly;
             *                      string ext;
             *
             *                      ExtractFileNameAndExt(filename, out nameonly, out ext);
             *
             *                      alterPath = string.Format("{0}({1}){2}", nameonly, count++, ext);
             *                      alterPath = GetFilePath(alterPath);
             *              }
             *              while (File.Exists(alterPath));
             *
             *              path = alterPath;
             *      }
             * }
             * break;
             */
            // 사라진 옵션이니 FileNameOrUrl로 대체

            case FileNameMakingMethod.FileNameOrUrl:
            {
                // by filename first
                path = MakeFilePath(url, FileNameMakingMethod.FileName);

                // check dup
                if (File.Exists(path))
                {
                    Logger.DLog("file already exists, try url");

                    // by url
                    path = MakeFilePath(url, FileNameMakingMethod.FullUrl);
                }
            }
            break;
            }

            return(path);
        }