Пример #1
0
        public string GetPageElementImageUrl(long Id, PageElement element, string login)
        {
            Paths paths = GetLoginImagePaths(login);

            if (paths == null)
            {
                return(null);
            }

            paths.Absolute = Path.Combine(paths.Absolute, element.ToString());

            if (!Directory.Exists(paths.Absolute))
            {
                return(null);
            }

            List <string> files = Directory.GetFiles(paths.Absolute, Id.ToString() + ".*", SearchOption.TopDirectoryOnly).ToList();

            files.AddRange(Directory.GetFiles(paths.Absolute, Id.ToString() + "_.*", SearchOption.TopDirectoryOnly));

            if (!files.Any())
            {
                return(null);
            }

            paths.Relative = Path.Combine(paths.Relative, element.ToString());

            string result = Path.Combine(paths.Relative, Path.GetFileName(files[0]));

            result = result.Replace('\\', '/');
            return(result);
        }
Пример #2
0
        // TODO: reduce login and page names down to reasonable minimum, because path length is limited wih 248 (?) characters
        public async Task <string> UploadElementImage(string login, PageElement pageElement, IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                es.ThrowException("File is empty");
            }

            if (string.IsNullOrWhiteSpace(options.Value.AccountImagesPath))
            {
                es.ThrowException("No account images path in configuration");
            }

            if (string.IsNullOrWhiteSpace(login))
            {
                es.ThrowException("No login to save an image to");
            }

            Tuple <string, string> ext = imageTypes.FirstOrDefault(x => string.Equals(x.Item1, file.ContentType, StringComparison.OrdinalIgnoreCase));

            if (ext == null)
            {
                es.ThrowInfoException("File to upload is not an image");
            }

            if (file.Length > options.Value.Images.InputSizeLimit * 1024 * 1024)
            {
                es.ThrowInfoException("File to upload exceeds a limit {0} Mb",
                                      options.Value.Images.InputSizeLimit.ToString());
            }

            string relativePath = Path.Combine(options.Value.AccountImagesPath, login, pageElement.ToString());
            string path         = Path.Combine(environment.WebRootPath, relativePath);

            Directory.CreateDirectory(path);

            List <string> files =
                Directory
                .GetFiles(path)
                .Where(x => Path.GetFileNameWithoutExtension(x) == file.FileName ||
                       Path.GetFileNameWithoutExtension(x) == file.FileName + "_")
                .ToList();

            bool add_ = files.Count > 0 && !files.Any(x => Path.GetFileNameWithoutExtension(x) == file.FileName + "_");

            files.ForEach(x => File.Delete(x));

            string fileName = file.FileName + (add_ ? "_" : "") + "." + ext.Item2;

            using (var fileStream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
            {
                if (file.Length > options.Value.Images.ConvertSizeLimit * 1024)
                {
                    reduceImageSize(file, fileStream);
                }
                else
                {
                    await file.CopyToAsync(fileStream);
                }
            }

            return(Path.Combine(relativePath, fileName).Replace('\\', '/'));
        }
Пример #3
0
        public void DeleteElementImage(long id, string login, PageElement pageElement)
        {
            string path = Path.Combine(environment.WebRootPath, options.Value.AccountImagesPath, login, pageElement.ToString());

            if (!Directory.Exists(path))
            {
                return;
            }

            Directory
            .GetFiles(path)
            .Where(x =>
                   Path.GetFileNameWithoutExtension(x) == id.ToString() ||
                   Path.GetFileNameWithoutExtension(x) == id.ToString() + "_"
                   )
            .ToList()
            .ForEach(x => File.Delete(x));
        }