コード例 #1
0
        private Photo SaveImage(
            Stream stream,
            int width,
            int height,
            string resizeName,
            string imageFileName,
            string mimeType = "image/jpeg")
        {
            string physicalPath = System.Web.HttpContext.Current.Server.MapPath(_filePath);

            if (!string.IsNullOrWhiteSpace(resizeName))
            {
                physicalPath = Path.Combine(physicalPath, resizeName);
            }

            // Create a directory for the specified resizeName if not exists
            if (!Directory.Exists(physicalPath))
            {
                Directory.CreateDirectory(physicalPath);
                GrantAccess(physicalPath);
            }

            string fileName = Path.Combine(physicalPath, imageFileName);

            using (var resizedImage = ImageHelper.ResizeImage(stream, width, height))
            {
                ImageHelper.SaveImage(fileName, resizedImage, 90, mimeType);
            }

            // todo: again, need to isolate the logic to get file path, shouldn't be part of this function
            string extension = GetExtension(mimeType);
            string resizeFolder = resizeName != null ? string.Format("{0}/", resizeName) : string.Empty;

            var photo = new Photo
            {
                Id = imageFileName.Replace(extension, string.Empty).Trim('.'),
                ResizeName = resizeName,
                Url = string.Format("{0}/{1}{2}", _filePath.TrimEnd('/'), resizeFolder, imageFileName)
            };

            return photo;
        }
コード例 #2
0
        private Photo GetPhoto(string id, string resizeName)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                return null;
            }

            Photo photo = null;
            string physicalPath = System.Web.HttpContext.Current.Server.MapPath(_filePath);

            if (!string.IsNullOrWhiteSpace(resizeName))
            {
                physicalPath = Path.Combine(physicalPath, resizeName);
            }

            if (Directory.Exists(physicalPath))
            {
                var path = Path.Combine(physicalPath, id);

                // todo: need to throw exception if file doesnt exist at all..
                var file = new FileInfo(path);

                // if id doesn't contain extension then search by file name
                if (!file.Exists)
                {
                    file = new DirectoryInfo(physicalPath).GetFiles().FirstOrDefault(c => c.Name.StartsWith(id));
                }

                if (file != null)
                {
                    string resizeFolder = resizeName != null ? string.Format("{0}/", resizeName) : string.Empty;
                    string virtualImagePath = string.Format("{0}/{1}{2}", _filePath.TrimEnd('/'), resizeFolder, file.Name);

                    photo = new Photo { Id = file.Name.Split('.')[0], ResizeName = resizeName, Url = virtualImagePath };
                }
            }

            return photo;
        }