forked from ddrilling/AsbCloudServer
CS2-106: Fixed Drilling program creation and preview logic
This commit is contained in:
parent
e8ad17bc1d
commit
45b4d8bb23
@ -11,6 +11,7 @@ namespace AsbCloudApp.Data
|
||||
public string Name { get; set; }
|
||||
public DateTime UploadDate { get; set; }
|
||||
public long Size { get; set; }
|
||||
public FilePublishInfoDto PublishInfo { get; set; }
|
||||
public UserDto Author { get; set; }
|
||||
}
|
||||
}
|
||||
|
11
AsbCloudApp/Data/FilePublishInfoDto.cs
Normal file
11
AsbCloudApp/Data/FilePublishInfoDto.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace AsbCloudApp.Data
|
||||
{
|
||||
public class FilePublishInfoDto
|
||||
{
|
||||
public string PublisherLogin { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
public string WebStorageFileUrl { get; set; }
|
||||
}
|
||||
}
|
@ -11,12 +11,9 @@ namespace AsbCloudApp.Services
|
||||
{
|
||||
string RootPath { get; }
|
||||
|
||||
Task<string> GetProgramWebUrlAsync(int idWell,
|
||||
CancellationToken token = default);
|
||||
Task<string> PublishFileToCloudAsync(string filePath, string originalName,
|
||||
CancellationToken token = default);
|
||||
Task<int> SaveWeblinkToFileInfo(int idFileInfo, string weblink, CancellationToken token = default);
|
||||
|
||||
Task<string> GetFileWebUrlAsync(FileInfoDto dto, string userLogin,
|
||||
string relativePath, CancellationToken token = default);
|
||||
|
||||
Task<FileInfoDto> SaveAsync(int idWell, int? idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default);
|
||||
|
||||
Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
|
||||
|
2700
AsbCloudDb/Migrations/20211029072655_Add_PublishInfo_To_FileInfo.Designer.cs
generated
Normal file
2700
AsbCloudDb/Migrations/20211029072655_Add_PublishInfo_To_FileInfo.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,36 @@
|
||||
using AsbCloudDb.Model;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace AsbCloudDb.Migrations
|
||||
{
|
||||
public partial class Add_PublishInfo_To_FileInfo : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "web_storage_url",
|
||||
table: "t_file_info");
|
||||
|
||||
migrationBuilder.AddColumn<FilePublishInfo>(
|
||||
name: "publish_info",
|
||||
table: "t_file_info",
|
||||
type: "jsonb",
|
||||
nullable: true,
|
||||
comment: "Информация о файле в облаке");
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "publish_info",
|
||||
table: "t_file_info");
|
||||
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "web_storage_url",
|
||||
table: "t_file_info",
|
||||
type: "text",
|
||||
nullable: true,
|
||||
comment: "Ссылка для просмотра файла в облаке");
|
||||
}
|
||||
}
|
||||
}
|
@ -511,6 +511,11 @@ namespace AsbCloudDb.Migrations
|
||||
.HasColumnName("name")
|
||||
.HasComment("Название файла");
|
||||
|
||||
b.Property<FilePublishInfo>("PublishInfo")
|
||||
.HasColumnType("jsonb")
|
||||
.HasColumnName("publish_info")
|
||||
.HasComment("Информация о файле в облаке");
|
||||
|
||||
b.Property<long>("Size")
|
||||
.HasColumnType("bigint")
|
||||
.HasColumnName("file_size")
|
||||
@ -520,11 +525,6 @@ namespace AsbCloudDb.Migrations
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("date");
|
||||
|
||||
b.Property<string>("WebStorageUrl")
|
||||
.HasColumnType("text")
|
||||
.HasColumnName("web_storage_url")
|
||||
.HasComment("Ссылка для просмотра файла в облаке");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("IdAuthor");
|
||||
|
@ -31,8 +31,8 @@ namespace AsbCloudDb.Model
|
||||
[Column("file_size"), Comment("Размер файла")]
|
||||
public long Size { get; set; }
|
||||
|
||||
[Column("web_storage_url"), Comment("Ссылка для просмотра файла в облаке")]
|
||||
public string WebStorageUrl { get; set; }
|
||||
[Column("publish_info", TypeName = "jsonb"), Comment("Информация о файле в облаке")]
|
||||
public FilePublishInfo PublishInfo { get; set; }
|
||||
|
||||
[Column("is_deleted"), Comment("Удален ли файл")]
|
||||
public bool IsDeleted { get; set; }
|
||||
|
11
AsbCloudDb/Model/FilePublishInfo.cs
Normal file
11
AsbCloudDb/Model/FilePublishInfo.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace AsbCloudDb.Model
|
||||
{
|
||||
public class FilePublishInfo
|
||||
{
|
||||
public string PublisherLogin { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
public string WebStorageFileUrl { get; set; }
|
||||
}
|
||||
}
|
@ -32,39 +32,14 @@ namespace AsbCloudInfrastructure.Services
|
||||
this.googleDriveService = googleDriveService;
|
||||
}
|
||||
|
||||
public async Task<string> GetProgramWebUrlAsync(int idWell,
|
||||
CancellationToken token = default)
|
||||
public async Task<string> GetFileWebUrlAsync(FileInfoDto dto, string userLogin,
|
||||
string relativePath, CancellationToken token = default)
|
||||
{
|
||||
var fileInfo = await db.Files.FirstOrDefaultAsync(f => f.IdWell == idWell &&
|
||||
f.IdCategory == 14, token)
|
||||
.ConfigureAwait(false);
|
||||
var fileWebLink = await PublishFileToCloudAsync(relativePath,
|
||||
dto.Name, token);
|
||||
await SaveWeblinkToFileInfo(dto.Id, userLogin, fileWebLink, token);
|
||||
|
||||
return fileInfo?.WebStorageUrl;
|
||||
}
|
||||
|
||||
public async Task<string> PublishFileToCloudAsync(string filePath, string originalName,
|
||||
CancellationToken token = default)
|
||||
{
|
||||
await using var fileStream = File.Open(filePath, FileMode.Open);
|
||||
var uploadedFileId = await googleDriveService.UploadFileAsync(fileStream, originalName,
|
||||
"", "uploaded", token)
|
||||
.ConfigureAwait(false);
|
||||
await googleDriveService.CreatePublicPermissionForFileAsync(uploadedFileId, token)
|
||||
.ConfigureAwait(false);
|
||||
var webLink = await googleDriveService.GetFileWebLinkAsync(uploadedFileId, token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return webLink;
|
||||
}
|
||||
|
||||
public async Task<int> SaveWeblinkToFileInfo(int idFileInfo, string weblink,
|
||||
CancellationToken token = default)
|
||||
{
|
||||
var fileInfo = await db.Files.FirstOrDefaultAsync(f => f.Id == idFileInfo, token)
|
||||
.ConfigureAwait(false);
|
||||
fileInfo.WebStorageUrl = weblink;
|
||||
|
||||
return await db.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
return fileWebLink;
|
||||
}
|
||||
|
||||
public async Task<FileInfoDto> MoveAsync(int idWell, int? idUser, int idCategory, string destinationFileName, string srcFilePath, CancellationToken token = default)
|
||||
@ -262,6 +237,35 @@ namespace AsbCloudInfrastructure.Services
|
||||
|
||||
public string GetUrl(int idWell, int idCategory, int idFile, string dotExtention) =>
|
||||
Path.Combine(RootPath, idWell.ToString(), idCategory.ToString(), $"{idFile}{dotExtention}");
|
||||
|
||||
|
||||
private async Task<string> PublishFileToCloudAsync(string filePath, string originalName,
|
||||
CancellationToken token = default)
|
||||
{
|
||||
await using var fileStream = File.Open(filePath, FileMode.Open);
|
||||
var uploadedFileId = await googleDriveService.UploadFileAsync(fileStream, originalName,
|
||||
"", "uploaded", token)
|
||||
.ConfigureAwait(false);
|
||||
await googleDriveService.CreatePublicPermissionForFileAsync(uploadedFileId, token)
|
||||
.ConfigureAwait(false);
|
||||
var webLink = await googleDriveService.GetFileWebLinkAsync(uploadedFileId, token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return webLink;
|
||||
}
|
||||
|
||||
private async Task<int> SaveWeblinkToFileInfo(int idFileInfo, string userLogin, string weblink,
|
||||
CancellationToken token = default)
|
||||
{
|
||||
var fileInfo = await db.Files.FirstOrDefaultAsync(f => f.Id == idFileInfo, token)
|
||||
.ConfigureAwait(false);
|
||||
fileInfo.PublishInfo = new FilePublishInfo()
|
||||
{
|
||||
PublisherLogin = userLogin,
|
||||
Date = DateTime.Now,
|
||||
WebStorageFileUrl = weblink
|
||||
};
|
||||
|
||||
return await db.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -37,22 +37,12 @@ namespace AsbCloudWebApi.Controllers
|
||||
return NoContent();
|
||||
|
||||
var relativePath = fileService.GetUrl(fileInfo);
|
||||
// Чтоб не смешивать этот временный функционал с основным публикация на гугл диск вся вынесена сюда
|
||||
// и выполняется уже после всех необходимых действий по созданию файла программы бурения.
|
||||
var fileWebLink = await fileService.GetProgramWebUrlAsync(idWell, token);
|
||||
|
||||
if (string.IsNullOrEmpty(fileWebLink))
|
||||
{
|
||||
fileWebLink = await fileService.PublishFileToCloudAsync(relativePath,
|
||||
fileInfo.Name, token);
|
||||
await fileService.SaveWeblinkToFileInfo(fileInfo.Id, fileWebLink, token);
|
||||
}
|
||||
|
||||
return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Возвращает ссылку на файл программы бурения в облаке
|
||||
/// Создает программу бурения
|
||||
/// </summary>
|
||||
/// <param name="idWell"> id скважины </param>
|
||||
/// <param name="token"> Токен отмены задачи </param>
|
||||
@ -61,9 +51,23 @@ namespace AsbCloudWebApi.Controllers
|
||||
[ProducesResponseType(typeof(string), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> GetFileWebLinkAsync(int idWell, CancellationToken token = default)
|
||||
{
|
||||
var fileWebLink = await fileService.GetProgramWebUrlAsync(idWell, token);
|
||||
var fileInfo = await drillingProgramService.GetAsync(idWell, token)
|
||||
.ConfigureAwait(false);
|
||||
if (fileInfo is null)
|
||||
return NoContent();
|
||||
|
||||
return Ok(fileWebLink);
|
||||
var fileWebUrl = fileInfo.PublishInfo?.WebStorageFileUrl;
|
||||
|
||||
if (!string.IsNullOrEmpty(fileWebUrl))
|
||||
return Ok(fileWebUrl);
|
||||
|
||||
var relativePath = fileService.GetUrl(fileInfo);
|
||||
var userLogin = User.Identity?.Name ?? "";
|
||||
|
||||
fileWebUrl = await fileService.GetFileWebUrlAsync(fileInfo,
|
||||
userLogin, relativePath, token);
|
||||
|
||||
return Ok(fileWebUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user