Ejemplo n.º 1
0
        // TODO: Put strings into the resources.
        //
        protected virtual Task <IVh <StreamInfo> > GetSourceStreamAsync(ISubjectFileLoadContext ctx)
        {
            var taskProxy = default(TaskCompletionSource <IVh <StreamInfo> >);

            try {
                taskProxy = new TaskCompletionSource <IVh <StreamInfo> >();
                //
                ctx.EnsureNotNull(nameof(ctx));
                //
                Uri readUri;
                try {
                    readUri = ctx.BaseUri.Arg($"{nameof(ctx)}.{nameof(ctx.BaseUri)}").EnsureAbsolute().EnsureScheme(UriUtilities.UriSchemeFile).EnsureLoopbackOrUnc().Value;
                }
                catch (Exception exception) {
                    throw
                        new ArgumentException(
                            message: $"Указанный контекст загрузки имеет недопустимый базовый URI загрузки (свойство '{nameof(ctx)}.{nameof(ctx.BaseUri)}').{Environment.NewLine}\tИмя параметра:{Environment.NewLine}{nameof(ctx).IndentLines2()}{Environment.NewLine}\tКонтекст загрузки (параметр):{Environment.NewLine}{ctx.FmtStr().G().IndentLines2()}",
                            innerException: exception);
                }
                //
                var file = new FileInfo(readUri.LocalPath);
                if (file.Exists)
                {
                    taskProxy
                    .SetResult(
                        result:
                        new StreamInfo(
                            stream: new FileStream(path: file.FullName, mode: FileMode.Open, access: FileAccess.Read, share: FileShare.Read),
                            ownsStream: true)
                        .ToValueHolder(ownsValue: true));
                }
                else
                {
                    throw
                        new FileNotFoundException(
                            message: FormatXResource(locator: typeof(FileNotFoundException), subpath: null, args: new string[] { file.FullName }),
                            fileName: file.FullName);
                }
                //
                return(taskProxy.Task);
            }
            catch (Exception exception) {
                if (taskProxy?.TrySetException(exception) == true)
                {
                    return(taskProxy.Task);
                }
                else
                {
                    return(Task.FromException <IVh <StreamInfo> >(exception));
                }
            }
        }
Ejemplo n.º 2
0
 protected override async Task <IVh <StreamInfo> > RecognizeMediaFormatAsync(ISubjectFileLoadContext ctx, Stream stream)
 {
     ctx.EnsureNotNull(nameof(ctx));
     //
     if (ctx.File.Extension.EqualsOrdinalCI(DescriptionPackageConstants.PackageJsonFormatFileExtension))
     {
         return(new StreamInfo(stream: stream, ownsStream: false, contentMediaType: MediaTypeNameUtilities.AppJson).ToValueHolder());
     }
     else if (ctx.File.Extension.EqualsOrdinalCI(DescriptionPackageConstants.PackageXmlFormatFileExtension))
     {
         return(new StreamInfo(stream: stream, ownsStream: false, contentMediaType: MediaTypeNameUtilities.AppXml).ToValueHolder());
     }
     else
     {
         return(await base.RecognizeMediaFormatAsync(ctx, stream).ConfigureAwait(false));
     }
 }
Ejemplo n.º 3
0
 protected virtual Task <TSubject> DoMaterializeAsync(ISubjectFileLoadContext context, StreamInfo stream)
 => base.DoMaterializeAsync(context, stream);
Ejemplo n.º 4
0
 protected virtual Task <IVh <StreamInfo> > RecognizeMediaFormatAsync(ISubjectFileLoadContext ctx, Stream stream)
 => base.RecognizeMediaFormatAsync(ctx, stream);