Пример #1
0
 public static void FilePath_PreSaving(FilePathEntity fp, ref bool graphModified)
 {
     if (fp.IsNew && !unsafeMode.Value)
     {
         fp.FileType.GetAlgorithm().SaveFile(fp);
     }
 }
Пример #2
0
 public static void FilePath_PreSaving(FilePathEntity fp, PreSavingContext ctx)
 {
     if (fp.IsNew && !unsafeMode.Value)
     {
         var alg = fp.FileType.GetAlgorithm();
         alg.ValidateFile(fp);
         alg.SaveFile(fp);
     }
 }
 private DocSetEntity ZTokenStructureToDocSetEntity(TokenEntity token, FilePathEntity filepath, string anchor, TokenTypeEntity tokenType)
 {
     return(new DocSetEntity
     {
         ID = token.Id,
         Name = token.Name,
         Type = tokenType.Name,
         Path = filepath.Path + "#" + anchor
     });
 }
Пример #4
0
    public static void FilePath_PreSaving(FilePathEntity fp, PreSavingContext ctx)
    {
        if (fp.IsNew && !unsafeMode.Value)
        {
            var alg = fp.FileType.GetAlgorithm();
            alg.ValidateFile(fp);
            var task = alg.SaveFileAsync(fp);

            Transaction.PreRealCommit += data =>
            {
                var a = fp; //For ebuggin
                task.Wait();
            };
        }
    }
Пример #5
0
 public static WebImage?WebImage(this FilePathEntity fp) =>
 As.Expression(() => fp == null ? null ! : new WebImage {
Пример #6
0
        public static void Start(bool file, bool embeddedFile, bool filePath, bool embeddedFilePath)
        {
            if (Navigator.Manager.NotDefined(MethodInfo.GetCurrentMethod()))
            {
                Navigator.RegisterArea(typeof(FilesClient));

                UrlsRepository.DefaultSFUrls.AddRange(new Dictionary <string, Func <UrlHelper, string> >
                {
                    { "uploadFile", url => url.Action <FileController>(fc => fc.Upload()) },
                    { "uploadDroppedFile", url => url.Action <FileController>(fc => fc.UploadDropped()) },
                });

                if (file)
                {
                    RegisterFileConstructor <FileEntity>(data =>
                    {
                        return(new FileEntity {
                            FileName = data.FileName, BinaryFile = data.Content
                        }.Save());
                    });

                    RegisterDownloadUrlConstructor <FileEntity>(fp =>
                    {
                        return(RouteHelper.New().Action((FileController fc) => fc.Download(new RuntimeInfo(fp).ToString())));
                    });

                    RegisterFileDownloadResult <FileEntity>(ri =>
                    {
                        FileEntity f = (FileEntity)ri.ToLite().Retrieve();

                        return(new StaticContentResult(f.BinaryFile, f.FileName));
                    });


                    var es = new EntitySettings <FileEntity>();
                    Navigator.AddSetting(es);

                    var baseMapping = (Mapping <FileEntity>)es.MappingLine.AsEntityMapping().RemoveProperty(fp => fp.BinaryFile);

                    es.MappingLine = ctx =>
                    {
                        RuntimeInfo runtimeInfo = ctx.GetRuntimeInfo();
                        if (runtimeInfo == null)
                        {
                            return(null);
                        }
                        else
                        {
                            if (runtimeInfo.IsNew)
                            {
                                HttpPostedFileBase hpf = GetHttpRequestFile(ctx);

                                if (hpf != null)
                                {
                                    return(new FileEntity
                                    {
                                        FileName = Path.GetFileName(hpf.FileName),
                                        BinaryFile = hpf.InputStream.ReadAllBytes()
                                    });
                                }
                                else
                                {
                                    throw new InvalidOperationException("Impossible to create new FileEntity {0}".FormatWith(ctx.Prefix));
                                }
                            }
                            else
                            {
                                return(baseMapping(ctx));
                            }
                        }
                    };

                    FileLogic.DownloadFileUrl = DownloadFileUrl;

                    var lm = new LiteMapping <FileEntity>();
                    lm.EntityHasChanges = ctx => ctx.GetRuntimeInfo().IsNew;
                    Mapping.RegisterValue <Lite <FileEntity> >(lm.GetValue);
                }

                if (embeddedFile)
                {
                    RegisterFileConstructor <FileEmbedded>(data =>
                    {
                        return(new FileEmbedded {
                            FileName = data.FileName, BinaryFile = data.Content
                        });
                    });

                    var es = new EmbeddedEntitySettings <FileEmbedded>();
                    Navigator.AddSetting(es);

                    var baseMapping = (Mapping <FileEmbedded>)es.MappingDefault.AsEntityMapping().RemoveProperty(fp => fp.BinaryFile);

                    es.MappingDefault = ctx =>
                    {
                        RuntimeInfo runtimeInfo = ctx.GetRuntimeInfo();
                        if (runtimeInfo == null)
                        {
                            return(null);
                        }
                        else
                        {
                            HttpPostedFileBase hpf = GetHttpRequestFile(ctx);

                            if (hpf != null && hpf.ContentLength != 0)
                            {
                                return(new FileEmbedded()
                                {
                                    FileName = Path.GetFileName(hpf.FileName),
                                    BinaryFile = hpf.InputStream.ReadAllBytes()
                                });
                            }
                            else if (ctx.Inputs.ContainsKey(EntityBaseKeys.EntityState))
                            {
                                return((FileEmbedded)Navigator.Manager.DeserializeEntity(ctx.Inputs[EntityBaseKeys.EntityState]));
                            }
                            else
                            {
                                return(baseMapping(ctx));
                            }
                        }
                    };
                }

                if (filePath)
                {
                    RegisterFileConstructor <FilePathEntity>(data =>
                    {
                        if (!data.FileType.HasText())
                        {
                            throw new InvalidOperationException("Couldn't create FilePath with unknown FileType for file '{0}'".FormatWith(data.FileName));
                        }

                        return(new FilePathEntity(SymbolLogic <FileTypeSymbol> .ToSymbol(data.FileType))
                        {
                            FileName = data.FileName,
                            BinaryFile = data.Content,
                            CalculatedDirectory = data.CalculatedDirectory
                        }.Save());
                    });

                    RegisterDownloadUrlConstructor <FilePathEntity>(fp =>
                    {
                        return(RouteHelper.New().Action((FileController fc) => fc.Download(new RuntimeInfo(fp).ToString())));
                    });

                    RegisterFileDownloadResult <FilePathEntity>(ri =>
                    {
                        FilePathEntity fp = (FilePathEntity)ri.ToLite().Retrieve();

                        return(new FilePathResult(fp.FullPhysicalPath(), MimeMapping.GetMimeMapping(fp.FullPhysicalPath()))
                        {
                            FileDownloadName = fp.FileName
                        });
                    });

                    Navigator.AddSettings(new List <EntitySettings>
                    {
                        new EntitySettings <FilePathEntity> {
                            PartialViewName = e => ViewPrefix.FormatWith("FilePath")
                        },
                    });

                    var es = Navigator.EntitySettings <FilePathEntity>();

                    var baseMapping = (Mapping <FilePathEntity>)es.MappingLine.AsEntityMapping().RemoveProperty(fp => fp.BinaryFile);

                    es.MappingLine = ctx =>
                    {
                        RuntimeInfo runtimeInfo = ctx.GetRuntimeInfo();
                        if (runtimeInfo == null)
                        {
                            return(null);
                        }
                        else
                        {
                            if (runtimeInfo.IsNew)
                            {
                                HttpPostedFileBase hpf = GetHttpRequestFile(ctx);
                                if (hpf != null)
                                {
                                    string fileType = ctx.Inputs[FileLineKeys.FileType];
                                    return(new FilePathEntity(SymbolLogic <FileTypeSymbol> .ToSymbol(fileType))
                                    {
                                        FileName = Path.GetFileName(hpf.FileName),
                                        BinaryFile = hpf.InputStream.ReadAllBytes(),
                                    });
                                }
                                else
                                {
                                    throw new InvalidOperationException("Impossible to create new FilePath {0}".FormatWith(ctx.Prefix));
                                }
                            }
                            else
                            {
                                return(baseMapping(ctx));
                            }
                        }
                    };

                    es.MappingMain = es.MappingLine;

                    var lm = new LiteMapping <FilePathEntity>();
                    lm.EntityHasChanges = ctx => ctx.GetRuntimeInfo().IsNew;
                    Mapping.RegisterValue <Lite <FilePathEntity> >(lm.GetValue);
                }

                if (embeddedFilePath)
                {
                    RegisterFileConstructor <FilePathEmbedded>(data =>
                    {
                        if (!data.FileType.HasText())
                        {
                            throw new InvalidOperationException("Couldn't create FilePath with unknown FileType for file '{0}'".FormatWith(data.FileName));
                        }

                        return(new FilePathEmbedded(SymbolLogic <FileTypeSymbol> .ToSymbol(data.FileType))
                        {
                            FileName = data.FileName,
                            BinaryFile = data.Content,
                            CalculatedDirectory = data.CalculatedDirectory
                        }.SaveFile());
                    });

                    RegisterDownloadUrlConstructor <FilePathEmbedded>(fp =>
                    {
                        return(RouteHelper.New().Action((FileController fc) => fc.DownloadEmbedded(fp.FileType.ToLite(), fp.Suffix, fp.FileName)));
                    });

                    Navigator.AddSettings(new List <EntitySettings>
                    {
                        new EmbeddedEntitySettings <FilePathEmbedded> {
                            PartialViewName = e => ViewPrefix.FormatWith("EmbeddedFilePath")
                        },
                    });

                    var es = Navigator.EmbeddedEntitySettings <FilePathEmbedded>();

                    var baseMapping = (Mapping <FilePathEmbedded>)es.MappingDefault.AsEntityMapping().RemoveProperty(fp => fp.BinaryFile);

                    es.MappingDefault = ctx =>
                    {
                        RuntimeInfo runtimeInfo = ctx.GetRuntimeInfo();
                        if (runtimeInfo == null)
                        {
                            return(null);
                        }
                        else
                        {
                            HttpPostedFileBase hpf = GetHttpRequestFile(ctx);
                            if (hpf != null)
                            {
                                string fileType = ctx.Inputs[FileLineKeys.FileType];
                                return(new FilePathEmbedded(SymbolLogic <FileTypeSymbol> .ToSymbol(fileType))
                                {
                                    FileName = Path.GetFileName(hpf.FileName),
                                    BinaryFile = hpf.InputStream.ReadAllBytes(),
                                });
                            }
                            else if (ctx.Inputs.ContainsKey(EntityBaseKeys.EntityState))
                            {
                                return((FilePathEmbedded)Navigator.Manager.DeserializeEntity(ctx.Inputs[EntityBaseKeys.EntityState]));
                            }

                            else
                            {
                                return(baseMapping(ctx));
                            }
                        }
                    };
                }



                if (filePath || embeddedFilePath)
                {
                    Navigator.AddSetting(new EntitySettings <FileTypeSymbol>());
                }

                var dqm = DynamicQueryManager.Current;

                QuerySettings.FormatRules.Add(new FormatterRule("WebImage",
                                                                col => col.Type == typeof(WebImage),
                                                                col => new CellFormatter((help, obj) =>
                                                                                         (obj == null || ((WebImage)obj)?.FullWebPath.HasText() == false) ? null :
                                                                                         new HtmlTag("img")
                                                                                         .Attr("src", RouteHelper.New().Content(((WebImage)obj).FullWebPath))
                                                                                         .Attr("alt", typeof(WebImage).NiceName())
                                                                                         .Attr("style", "width:80px").ToHtmlSelf())
                {
                    TextAlign = "center"
                }
                                                                ));

                QuerySettings.FormatRules.Add(new FormatterRule("WebDownload",
                                                                col => col.Type == typeof(WebDownload),
                                                                col => new CellFormatter((help, obj) => ((WebDownload)obj)?.FullWebPath.HasText() == false ? null :
                                                                                         new MvcHtmlString("<a href='{0}'>{1}</a>".FormatWith(RouteHelper.New().Content(((WebDownload)obj).FullWebPath), ((WebDownload)obj).FileName ?? typeof(WebDownload).NiceName())))
                {
                    TextAlign = "center"
                }
                                                                ));
            }
        }
Пример #7
0
 static PrefixPair CalculatePrefixPair(FilePathEntity fp)
 {
     using (new EntityCache(EntityCacheType.ForceNew))
         return(fp.FileType.GetAlgorithm().GetPrefixPair(fp));
 }
Пример #8
0
 public static Stream OpenRead(this FilePathEntity fp)
 {
     return(fp.FileType.GetAlgorithm().OpenRead(fp));
 }
Пример #9
0
 public static byte[] GetByteArray(this FilePathEntity fp)
 {
     return(fp.BinaryFile ?? fp.FileType.GetAlgorithm().ReadAllBytes(fp));
 }
Пример #10
0
 static void FilePathLogic_Retrieved(FilePathEntity fp)
 {
     fp.GetPrefixPair();
 }
Пример #11
0
 public static WebDownload WebDownload(this FilePathEntity fp)
 {
     return(WebDownloadExpression.Evaluate(fp));
 }
Пример #12
0
 public static WebImage?WebImage(this FilePathEntity fp)
 {
     return(WebImageExpression.Evaluate(fp));
 }