iluvadev / PocketBaseClient

C# client to interact with a particular PocketBase application: an ORM mapped to your PocketBase server. [This project is in active development. The things described below could change]
MIT License
39 stars 8 forks source link

update user avatar file get error:validation_unknown_filenames #44

Open neozhu opened 4 months ago

neozhu commented 4 months ago

image image

'{"code":400,"message":"Failed to load the submitted data due to invalid formatting.","data":{"avatar":{"code":"validation_unknown_filenames","message":"The field contains unknown filenames."}}} '

neozhu commented 4 months ago
internal static async Task<T?> HttpPatchAsync<T>(this PocketBase pocketBase, string url, T item)
    where T : ItemBase
{
    var body = JsonSerializer.Deserialize<Dictionary<string, object>>(JsonSerializer.Serialize(item));
    var files = item.RelatedFiles.Where(f => f != null && f.HasChanges)?.Select(f => f!.GetSdkFileToUpload()).Where(f => f != null).Select(f => f!)?.ToList();

    if (files?.Any() ?? false)
    {
        await pocketBase.SendAsync<T>(url, HttpMethod.Patch, files: files);
    }
    // remove FieldFileBase field for fix The field contains unknown filenames.
    foreach (var property in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
             .Where(prop => prop.PropertyType.IsSubclassOf(typeof(FieldFileBase))))
    {
        var jsonPropertyNameAttribute = property.GetCustomAttribute<JsonPropertyNameAttribute>();
        if (jsonPropertyNameAttribute is not null)
        {
            body.Remove(jsonPropertyNameAttribute.Name);
        }
    }
    var result = await pocketBase.SendAsync<T>(url, HttpMethod.Patch, body: body);
    return result;
}