Skip to content

Commit

Permalink
Do not dispose client after file download.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Aug 8, 2024
1 parent 90d57cc commit 6f450ea
Show file tree
Hide file tree
Showing 6 changed files with 614 additions and 142 deletions.
2 changes: 1 addition & 1 deletion cli/Squidex.CLI/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
<PackageTags>Squidex HeadlessCMS</PackageTags>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>13.0</Version>
<Version>13.1</Version>
</PropertyGroup>
</Project>
7 changes: 6 additions & 1 deletion cli/Squidex.CLI/Squidex.CLI/Commands/App_Backup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public async Task Create(CreateArguments arguments)
{
log.WriteLine("Backup completed. Downloading...");

await using (var fs = new FileStream(arguments.File, FileMode.CreateNew))
var mode = arguments.Force ? FileMode.Create : FileMode.CreateNew;

await using (var fs = new FileStream(arguments.File, mode))
{
#pragma warning disable CS0612 // Type or member is obsolete
using (var download = await session.Client.Backups.GetBackupContentAsync(foundBackup.Id))
Expand Down Expand Up @@ -99,6 +101,9 @@ public sealed class CreateArguments : AppArguments
[Option("deleteAfterDownload", Description = "Defines if the created backup shall be deleted from app after the backup task is completed.")]
public bool DeleteAfterDownload { get; set; }

[Option("force", 'f', Description = "Overwrites the file if it already exists.")]
public bool Force { get; set; }

public sealed class Validator : AbstractValidator<CreateArguments>
{
public Validator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@

{% endif -%}
var client_ = _options.ClientProvider.Get();
#pragma warning disable CS0219 // Variable is assigned but its value is never used
var disposeClient_ = false;
try
{
Expand Down Expand Up @@ -444,6 +445,7 @@
finally
{
}
#pragma warning restore CS0219 // Variable is assigned but its value is never used
}

{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@
<None Update="Client.Class.liquid">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="File.liquid">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
204 changes: 204 additions & 0 deletions csharp/Squidex.ClientLibrary/CodeGeneration/File.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v{{ ToolchainVersion }} (http://NSwag.org)
// </auto-generated>
//----------------------

{% if GenerateNullableReferenceTypes -%}
#nullable enable

{%- endif %}
{% for usage in NamespaceUsages -%}
using {{ usage }};
{% endfor -%}

{% template File.Header %}

namespace {{ Namespace }}
{
using System = global::System;

{{ Clients | tab }}

{% if GenerateContracts -%}
{{ Classes | tab }}

{%- if RequiresFileParameterType -%}
[System.CodeDom.Compiler.GeneratedCode("NSwag", "{{ ToolchainVersion }}")]
public partial class FileParameter
{
public FileParameter(System.IO.Stream data)
: this (data, null, null)
{
}

{%- if GenerateNullableReferenceTypes -%}
public FileParameter(System.IO.Stream data, string? fileName)
{%- else -%}
public FileParameter(System.IO.Stream data, string fileName)
{%- endif -%}
: this (data, fileName, null)
{
}

{%- if GenerateNullableReferenceTypes -%}
public FileParameter(System.IO.Stream data, string? fileName, string? contentType)
{%- else -%}
public FileParameter(System.IO.Stream data, string fileName, string contentType)
{%- endif -%}
{
Data = data;
FileName = fileName;
ContentType = contentType;
}

public System.IO.Stream Data { get; private set; }

{%- if GenerateNullableReferenceTypes -%}
public string? FileName { get; private set; }

public string? ContentType { get; private set; }
{%- else -%}
public string FileName { get; private set; }

public string ContentType { get; private set; }
{%- endif -%}
}

{%- endif %}
{%- if GenerateFileResponseClass %}
[System.CodeDom.Compiler.GeneratedCode("NSwag", "{{ ToolchainVersion }}")]
public partial class FileResponse : System.IDisposable
{
{%- if GenerateNullableReferenceTypes -%}
private System.IDisposable? _client;
private System.IDisposable? _response;
{%- else -%}
private System.IDisposable _client;
private System.IDisposable _response;
{%- endif -%}

public int StatusCode { get; private set; }

public System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> Headers { get; private set; }

public System.IO.Stream Stream { get; private set; }

public bool IsPartial
{
get { return StatusCode == 206; }
}

{%- if GenerateNullableReferenceTypes -%}
public FileResponse(int statusCode, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.IO.Stream stream, System.IDisposable? client, System.IDisposable? response)
{%- else -%}
public FileResponse(int statusCode, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.IO.Stream stream, System.IDisposable client, System.IDisposable response)
{%- endif -%}
{
StatusCode = statusCode;
Headers = headers;
Stream = stream;
_client = client;
_response = response;
}

public void Dispose()
{
Stream.Dispose();
if (_response != null)
_response.Dispose();
}
}

{%- endif %}
{%- if WrapResponses and GenerateResponseClasses %}
{%- for responseClassName in ResponseClassNames %}
[System.CodeDom.Compiler.GeneratedCode("NSwag", "{{ ToolchainVersion }}")]
public partial class {{ responseClassName }}
{
public int StatusCode { get; private set; }

public System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> Headers { get; private set; }

public {{ responseClassName }}(int statusCode, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers)
{
StatusCode = statusCode;
Headers = headers;
}
}

[System.CodeDom.Compiler.GeneratedCode("NSwag", "{{ ToolchainVersion }}")]
public partial class {{ responseClassName }}<TResult> : {{ responseClassName }}
{
public TResult Result { get; private set; }

public {{ responseClassName }}(int statusCode, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, TResult result)
: base(statusCode, headers)
{
Result = result;
}
}

{%- endfor %}
{%- endif %}
{%- if GenerateExceptionClasses %}
{%- for exceptionClassName in ExceptionClassNames %}
[System.CodeDom.Compiler.GeneratedCode("NSwag", "{{ ToolchainVersion }}")]
public partial class {{ exceptionClassName }} : System.Exception
{
public int StatusCode { get; private set; }

{%- if GenerateNullableReferenceTypes -%}
public string? Response { get; private set; }
{%- else -%}
public string Response { get; private set; }
{%- endif -%}

public System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> Headers { get; private set; }

{%- if GenerateNullableReferenceTypes -%}
public {{ exceptionClassName }}(string message, int statusCode, string? response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Exception? innerException)
{%- else -%}
public {{ exceptionClassName }}(string message, int statusCode, string response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Exception innerException)
{%- endif -%}
: base(message + "\n\nStatus: " + statusCode + "\nResponse: \n" + ((response == null) ? "(null)" : response.Substring(0, response.Length >= 512 ? 512 : response.Length)), innerException)
{
StatusCode = statusCode;
Response = response;
Headers = headers;
}

public override string ToString()
{
return string.Format("HTTP Response: \n\n{0}\n\n{1}", Response, base.ToString());
}
}

[System.CodeDom.Compiler.GeneratedCode("NSwag", "{{ ToolchainVersion }}")]
public partial class {{ exceptionClassName }}<TResult> : {{ exceptionClassName }}
{
public TResult Result { get; private set; }

{%- if GenerateNullableReferenceTypes -%}
public {{ exceptionClassName }}(string message, int statusCode, string? response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, TResult result, System.Exception? innerException)
{%- else -%}
public {{ exceptionClassName }}(string message, int statusCode, string response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, TResult result, System.Exception innerException)
{%- endif -%}
: base(message, statusCode, response, headers, innerException)
{
Result = result;
}
}

{% endfor -%}
{% endif -%}
{%- endif %}
{% if GenerateImplementation -%}
{% if RequiresJsonExceptionConverter -%}
{% template JsonExceptionConverter %}
{% endif -%}
{%- endif %}

}

{% template File.Footer %}
Loading

0 comments on commit 6f450ea

Please sign in to comment.