private IEnumerable <IFile> GetFiles(string path, string type)
        {
            var files = _fileSystem.ListFiles(GetRelativePath(path));

            if (type.IsEmpty() || type == "#")
            {
                return(files);
            }

            type = type.ToLowerInvariant();
            bool predicate(IFile x) => _mediaTypeResolver.Resolve(x.Extension) == type;

            return(files.Where(predicate));
        }
예제 #2
0
        public virtual MediaFile InsertPicture(
            byte[] pictureBinary,
            string mimeType,
            string fileName,
            int width,
            int height,
            bool isTransient = true,
            string album     = null)
        {
            var mime = mimeType.EmptyNull().Truncate(100);
            var name = NormalizeFileName(fileName, ref mime, out string ext);

            var file = new MediaFile
            {
                MimeType    = mime,
                Extension   = ext,
                Name        = name,
                IsTransient = isTransient
            };

            file.MediaType = _mediaTypeResolver.Resolve(file);

            if (album.HasValue())
            {
                var albumId = _albumRegistry.GetAlbumByName(album)?.Id;
                if (albumId > 0)
                {
                    file.FolderId = albumId;
                }
            }

            var stream = pictureBinary?.ToStream();

            file.RefreshMetadata(stream);
            _pictureRepository.Insert(file);

            // Save to storage.
            _storageProvider.Value.Save(file, stream);

            return(file);
        }
예제 #3
0
        protected async Task <(MediaStorageItem StorageItem, MediaFile File)> ProcessFile(
            MediaFile file,
            MediaPathData pathData,
            Stream inStream,
            bool isTransient = true,
            DuplicateFileHandling dupeFileHandling = DuplicateFileHandling.ThrowError,
            MimeValidationType mediaValidationType = MimeValidationType.MimeTypeMustMatch)
        {
            if (file != null)
            {
                if (dupeFileHandling == DuplicateFileHandling.ThrowError)
                {
                    var fullPath = pathData.FullPath;
                    await CheckUniqueFileName(pathData);

                    throw _exceptionFactory.DuplicateFile(fullPath, ConvertMediaFile(file, pathData.Folder), pathData.FullPath);
                }
                else if (dupeFileHandling == DuplicateFileHandling.Rename)
                {
                    if (await CheckUniqueFileName(pathData))
                    {
                        file = null;
                    }
                }
            }

            if (file != null && mediaValidationType != MimeValidationType.NoValidation)
            {
                if (mediaValidationType == MimeValidationType.MimeTypeMustMatch)
                {
                    ValidateMimeTypes("Save", file.MimeType, pathData.MimeType);
                }
                else if (mediaValidationType == MimeValidationType.MediaTypeMustMatch)
                {
                    ValidateMediaTypes("Save", _typeResolver.Resolve(pathData.Extension), file.MediaType);
                }

                // Restore file if soft-deleted
                file.Deleted = false;

                // Delete thumbnail
                await _imageCache.DeleteAsync(file);
            }

            file ??= new MediaFile
            {
                IsTransient = isTransient,
                FolderId    = pathData.Node.Value.Id
            };

            // Untrackable folders can never contain transient files.
            if (!pathData.Folder.CanDetectTracks)
            {
                file.IsTransient = false;
            }

            var name = pathData.FileName;

            if (name != pathData.FileName)
            {
                pathData.FileName = name;
            }

            file.Name      = pathData.FileName;
            file.Extension = pathData.Extension;
            file.MimeType  = pathData.MimeType;
            if (file.MediaType == null)
            {
                file.MediaType = _typeResolver.Resolve(pathData.Extension, pathData.MimeType);
            }

            // Process image
            if (inStream != null && inStream.Length > 0 && file.MediaType == MediaType.Image && (await ProcessImage(file, inStream)).Out(out var outImage))
            {
                file.Width     = outImage.Width;
                file.Height    = outImage.Height;
                file.PixelSize = outImage.Width * outImage.Height;

                return(MediaStorageItem.FromImage(outImage), file);
            }
            else
            {
                file.RefreshMetadata(inStream, _imageProcessor.Factory);

                return(MediaStorageItem.FromStream(inStream), file);
            }
        }
예제 #4
0
 public static MediaType Resolve(this IMediaTypeResolver resolver, MediaFile file)
 {
     Guard.NotNull(file, nameof(file));
     return(resolver.Resolve(file.Extension, file.MimeType));
 }