add chat and users
This commit is contained in:
@@ -208,5 +208,85 @@ public class DevicePostsController : ControllerBase
|
||||
var hasAccess = await _postService.CanUserAccessDeviceAsync(userId, deviceId, cancellationToken);
|
||||
return Ok(new { hasAccess });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ایجاد پست اسنپشات وضعیت سنسورها و رلهها
|
||||
/// </summary>
|
||||
[HttpPost("sensor-snapshot")]
|
||||
public async Task<ActionResult<int>> CreateSensorSnapshot(
|
||||
CreateSensorSnapshotRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var id = await _postService.CreateSensorSnapshotAsync(request, cancellationToken);
|
||||
return Ok(new { id, message = "وضعیت سنسورها و رلهها ارسال شد" });
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
return Unauthorized(new { error = ex.Message });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// آپلود فایل صوتی (ویس) برای پست
|
||||
/// </summary>
|
||||
[HttpPost("{postId}/voice")]
|
||||
[Consumes("multipart/form-data")]
|
||||
public async Task<ActionResult> UploadVoice(
|
||||
int postId,
|
||||
[FromForm] IFormFile file,
|
||||
[FromForm] int? durationSeconds,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (file == null || file.Length == 0)
|
||||
return BadRequest(new { error = "فایل انتخاب نشده است" });
|
||||
|
||||
var allowedTypes = new[]
|
||||
{
|
||||
"audio/webm", "audio/ogg", "audio/mp4", "audio/mpeg",
|
||||
"audio/wav", "audio/x-wav", "audio/amr", "audio/aac", "audio/m4a"
|
||||
};
|
||||
if (!allowedTypes.Contains(file.ContentType.ToLower()))
|
||||
return BadRequest(new { error = "فرمت فایل صوتی مجاز نیست" });
|
||||
|
||||
if (file.Length > 10 * 1024 * 1024)
|
||||
return BadRequest(new { error = "حجم فایل صوتی نباید بیشتر از 10 مگابایت باشد" });
|
||||
|
||||
var uploadsFolder = Path.Combine(_environment.WebRootPath ?? "wwwroot", "uploads", "voice");
|
||||
Directory.CreateDirectory(uploadsFolder);
|
||||
|
||||
var fileName = $"{Guid.NewGuid()}{Path.GetExtension(file.FileName)}";
|
||||
var filePath = Path.Combine(uploadsFolder, fileName);
|
||||
|
||||
await using (var stream = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
await file.CopyToAsync(stream, cancellationToken);
|
||||
}
|
||||
|
||||
var relativePath = $"/uploads/voice/{fileName}";
|
||||
await _postService.SetPostVoiceAsync(
|
||||
postId,
|
||||
file.FileName,
|
||||
relativePath,
|
||||
file.ContentType,
|
||||
file.Length,
|
||||
durationSeconds,
|
||||
cancellationToken);
|
||||
|
||||
return Ok(new { filePath = relativePath, message = "ویس با موفقیت آپلود شد" });
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
return NotFound(new { error = ex.Message });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error uploading voice for post {PostId}", postId);
|
||||
return StatusCode(500, new { error = "خطا در آپلود ویس" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user