Skip to content

Commit

Permalink
added C# function templates
Browse files Browse the repository at this point in the history
  • Loading branch information
revenz committed Aug 5, 2024
1 parent 174cf3a commit ea5ba0f
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 3 deletions.
5 changes: 5 additions & 0 deletions FileFlowsScriptRepo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@
<PackageReference Include="YamlDotNet" Version="15.1.2" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Scripts\Function\**\*.cs" />
<Content Include="Scripts\Function\**\*.cs" />
</ItemGroup>

</Project>
10 changes: 7 additions & 3 deletions Generators/RepoGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void Run()
repo.SystemScripts = GetScripts(Path.Combine(projDir, "Scripts/System"), ScriptType.System);
repo.FlowScripts = GetScripts(Path.Combine(projDir, "Scripts/Flow"), ScriptType.Flow);
repo.WebhookScripts = GetScripts(Path.Combine(projDir, "Scripts/Webhook"), ScriptType.Webhook);
repo.FunctionScripts = GetScripts(Path.Combine(projDir, "Scripts/Function"), ScriptType.Template);
repo.FunctionScripts = GetScripts(Path.Combine(projDir, "Scripts/Function"), ScriptType.Template, new [] { ".js", ".cs", ".bat", ".ps1", "sh"});
repo.FlowTemplates = GetTemplates(Path.Combine(projDir, "Templates/Flow"), community: false);
repo.CommunityFlowTemplates = GetTemplates(Path.Combine(projDir, "Templates/Flow"), community: true);
repo.LibraryTemplates = GetTemplates(Path.Combine(projDir, "Templates/Library"));
Expand All @@ -41,13 +41,17 @@ public static void Run()
Console.WriteLine("Done");
}

private static List<RepositoryObject> GetScripts(string path, ScriptType type)
private static List<RepositoryObject> GetScripts(string path, ScriptType type, string[] extensions = null)

Check warning on line 44 in Generators/RepoGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
extensions ??= new string[] { ".js" };
List<RepositoryObject> scripts = new List<RepositoryObject>();
var rgxComments = new Regex(@"\/\*(\*)?(.*?)\*\/", RegexOptions.Singleline);
var basePath = new DirectoryInfo(path);
foreach(var file in basePath.GetFiles("*.js", SearchOption.AllDirectories))
foreach(var file in basePath.GetFiles("*.*", SearchOption.AllDirectories))
{
if (extensions.Contains(file.Extension.ToLowerInvariant()) == false)
continue;

string content = File.ReadAllText(file.FullName);
var script = new RepositoryObject();

Expand Down
16 changes: 16 additions & 0 deletions Scripts/Function/File/File - Greater Than 1GB.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Checks if a file is larger than 1 GB
* @revision 1
* @outputs 2
* @minimumVersion 24.08.1.3441
*/

if(Variables.TryGetValue("file.Size", out var oSize) == false || oSize is long size == false)
{
Flow.FailureReason = "file.Size not set in variables";
Logger.ELog(Flow.FailureReason);
return -1;
}
if(size > 1_000_000_000)
return 1;
return 2;
57 changes: 57 additions & 0 deletions Scripts/Function/Video/Video - Bitrate Greater Than.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Checks if a video files bitrate is greater than a specific bitrate
* @revision 1
* @outputs 2
* @minimumVersion 24.08.1.3441
*/

// check if the bitrate for a video is over a certain amount
var MAX_BITRATE = 3_000_000; // bitrate is 3,000 KBps

if(Variables.TryGetValue("vi.VideoInfo", out var oVideoInfo) == false || oVideoInfo is FileFlows.VideoNodes.VideoInfo videoInfo == false)
{
Logger.ILog("Failed to locate VideoInformation in variables");
return -1;
}
Logger.ILog("Got video information.");

var video = videoInfo.VideoStreams.FirstOrDefault();
if(video == null)
{
Logger.ILog("No video streams detected.");
return -1;
}

// get the video stream
var bitrate = video.Bitrate;

if(bitrate < 1)
{
// video stream doesn't have bitrate information
// need to use the overall bitrate
var overall = videoInfo.Bitrate;
if(overall < 1)
return 0; // couldn't get overall bitrate either

// overall bitrate includes all audio streams, so we try and subtract those
var calculated = overall;
if(videoInfo.AudioStreams.Count > 0) // check there are audio streams
{
foreach(var audio in videoInfo.AudioStreams)
{
if(audio.Bitrate > 0)
calculated -= audio.Bitrate;
else{
// audio doesn't have bitrate either, so we just subtract 5% of the original bitrate
// this is a guess, but it should get us close
calculated -= (overall * 0.05f);
}
}
}
bitrate = calculated;
}

// check if the bitrate is over the maximum bitrate
if(bitrate > MAX_BITRATE)
return 1; // it is, so call output 1
return 2; // it isn't so call output 2
29 changes: 29 additions & 0 deletions Scripts/Function/Video/Video - Resolution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* A switch statement for video resolution
* @revision 1
* @outputs 4
* @minimumVersion 24.08.1.3441
*/

// get the first video stream, likely the only one
if(Variables.TryGetValue("vi.VideoInfo", out var oVideoInfo) == false || oVideoInfo is FileFlows.VideoNodes.VideoInfo videoInfo == false)
{
Logger.ILog("Failed to locate VideoInformation in variables");
return -1;
}
Logger.ILog("Got video information.");
var video = videoInfo.VideoStreams.FirstOrDefault();
if(video == null)
{
Logger.ILog("No video streams detected.");
return -1;
}

Logger.ILog("Video stream detected.");
if(video.Width > 3700)
return 1; // 4k
if(video.Width > 1800)
return 2; // 1080p
if(video.Width > 1200)
return 3; // 720p
return 4; // SD

0 comments on commit ea5ba0f

Please sign in to comment.