forked from ddrilling/AsbCloudServer
CS2-106: Added GoogleDriveService and .GetWebLink() to DrillingProgramController
This commit is contained in:
parent
45df5bb015
commit
ab13e97837
@ -11,6 +11,9 @@ namespace AsbCloudApp.Services
|
|||||||
{
|
{
|
||||||
string RootPath { get; }
|
string RootPath { get; }
|
||||||
|
|
||||||
|
string CreateGoogleDriveWebLink(string filePath, string originalName);
|
||||||
|
Task<int> SaveWeblinkToFileInfo(int idFileInfo, string weblink, CancellationToken token = default);
|
||||||
|
|
||||||
Task<FileInfoDto> SaveAsync(int idWell, int? idUser, int idCategory, string fileFullName, Stream fileStream, 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,
|
Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
|
||||||
|
13
AsbCloudApp/Services/IGoogleDriveService.cs
Normal file
13
AsbCloudApp/Services/IGoogleDriveService.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace AsbCloudApp.Services
|
||||||
|
{
|
||||||
|
public interface IGoogleDriveService
|
||||||
|
{
|
||||||
|
string GetFileWebLink(string idFile);
|
||||||
|
string CreateFolder(string folderName);
|
||||||
|
void CreatePublicPermissionForFile(string idFile);
|
||||||
|
string UploadFile(Stream file, string fileName, string fileMime, string fileDescription);
|
||||||
|
void DeleteFile(string fileId);
|
||||||
|
}
|
||||||
|
}
|
2700
AsbCloudDb/Migrations/20211027112525_Add_WebUrl_In_FileInfo.Designer.cs
generated
Normal file
2700
AsbCloudDb/Migrations/20211027112525_Add_WebUrl_In_FileInfo.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,24 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
namespace AsbCloudDb.Migrations
|
||||||
|
{
|
||||||
|
public partial class Add_WebUrl_In_FileInfo : Migration
|
||||||
|
{
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AddColumn<string>(
|
||||||
|
name: "web_storage_url",
|
||||||
|
table: "t_file_info",
|
||||||
|
type: "text",
|
||||||
|
nullable: true,
|
||||||
|
comment: "Ссылка для просмотра файла в облаке");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "web_storage_url",
|
||||||
|
table: "t_file_info");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -520,6 +520,11 @@ namespace AsbCloudDb.Migrations
|
|||||||
.HasColumnType("timestamp with time zone")
|
.HasColumnType("timestamp with time zone")
|
||||||
.HasColumnName("date");
|
.HasColumnName("date");
|
||||||
|
|
||||||
|
b.Property<string>("WebStorageUrl")
|
||||||
|
.HasColumnType("text")
|
||||||
|
.HasColumnName("web_storage_url")
|
||||||
|
.HasComment("Ссылка для просмотра файла в облаке");
|
||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
b.HasIndex("IdAuthor");
|
b.HasIndex("IdAuthor");
|
||||||
|
@ -30,6 +30,9 @@ namespace AsbCloudDb.Model
|
|||||||
|
|
||||||
[Column("file_size"), Comment("Размер файла")]
|
[Column("file_size"), Comment("Размер файла")]
|
||||||
public long Size { get; set; }
|
public long Size { get; set; }
|
||||||
|
|
||||||
|
[Column("web_storage_url"), Comment("Ссылка для просмотра файла в облаке")]
|
||||||
|
public string WebStorageUrl { get; set; }
|
||||||
|
|
||||||
[Column("is_deleted"), Comment("Удален ли файл")]
|
[Column("is_deleted"), Comment("Удален ли файл")]
|
||||||
public bool IsDeleted { get; set; }
|
public bool IsDeleted { get; set; }
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="ClosedXML" Version="0.95.4" />
|
<PackageReference Include="ClosedXML" Version="0.95.4" />
|
||||||
|
<PackageReference Include="Google.Apis.Drive.v3" Version="1.55.0.2481" />
|
||||||
<PackageReference Include="itext7" Version="7.1.15" />
|
<PackageReference Include="itext7" Version="7.1.15" />
|
||||||
<PackageReference Include="Mapster" Version="7.2.0" />
|
<PackageReference Include="Mapster" Version="7.2.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
||||||
|
@ -46,6 +46,7 @@ namespace AsbCloudInfrastructure
|
|||||||
services.AddTransient<IDrillingProgramApacheService, DrillingProgramApacheService>();
|
services.AddTransient<IDrillingProgramApacheService, DrillingProgramApacheService>();
|
||||||
services.AddTransient<IDrillParamsService, DrillParamsService>();
|
services.AddTransient<IDrillParamsService, DrillParamsService>();
|
||||||
services.AddTransient<IDrillFlowChartService, DrillFlowChartService>();
|
services.AddTransient<IDrillFlowChartService, DrillFlowChartService>();
|
||||||
|
services.AddTransient<IGoogleDriveService, GoogleDriveService>();
|
||||||
|
|
||||||
// admin crud services:
|
// admin crud services:
|
||||||
services.AddTransient<ICrudService<DepositDto>, CrudServiceBase<DepositDto, Deposit>>();
|
services.AddTransient<ICrudService<DepositDto>, CrudServiceBase<DepositDto, Deposit>>();
|
||||||
|
@ -18,8 +18,9 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
|
|
||||||
private readonly IQueryable<AsbCloudDb.Model.FileInfo> dbSetConfigured;
|
private readonly IQueryable<AsbCloudDb.Model.FileInfo> dbSetConfigured;
|
||||||
private readonly IAsbCloudDbContext db;
|
private readonly IAsbCloudDbContext db;
|
||||||
|
private readonly IGoogleDriveService googleDriveService;
|
||||||
|
|
||||||
public FileService(IAsbCloudDbContext db)
|
public FileService(IAsbCloudDbContext db, IGoogleDriveService googleDriveService)
|
||||||
{
|
{
|
||||||
RootPath = "files";
|
RootPath = "files";
|
||||||
this.db = db;
|
this.db = db;
|
||||||
@ -27,6 +28,30 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
.Include(f => f.Author)
|
.Include(f => f.Author)
|
||||||
.ThenInclude(u => u.Company)
|
.ThenInclude(u => u.Company)
|
||||||
.ThenInclude(c => c.CompanyType);
|
.ThenInclude(c => c.CompanyType);
|
||||||
|
|
||||||
|
this.googleDriveService = googleDriveService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string CreateGoogleDriveWebLink(string filePath, string originalName)
|
||||||
|
{ // TODO: Сделать проверку, есть ли уже такой файл на гугл диске. Если есть, то удалить и пересоздать.
|
||||||
|
// Без этого они будут копиться там с одинаковыми названиями. Или в найстроках гугл диска мож есть перезапись файлов с одинаковыми названиями.
|
||||||
|
using var fileStream = File.Open(filePath, FileMode.Open);
|
||||||
|
var uploadedFileId = googleDriveService.UploadFile(fileStream, originalName,
|
||||||
|
"", "uploaded");
|
||||||
|
googleDriveService.CreatePublicPermissionForFile(uploadedFileId);
|
||||||
|
var webLink = googleDriveService.GetFileWebLink(uploadedFileId);
|
||||||
|
|
||||||
|
return webLink;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<int> SaveWeblinkToFileInfo(int idFileInfo, string weblink,
|
||||||
|
CancellationToken token = default)
|
||||||
|
{
|
||||||
|
var fileInfo = await db.Files.FirstOrDefaultAsync(f => f.Id == idFileInfo)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
fileInfo.WebStorageUrl = weblink;
|
||||||
|
|
||||||
|
return await db.SaveChangesAsync(token).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<FileInfoDto> MoveAsync(int idWell, int? idUser, int idCategory, string destinationFileName, string srcFilePath, CancellationToken token = default)
|
public async Task<FileInfoDto> MoveAsync(int idWell, int? idUser, int idCategory, string destinationFileName, string srcFilePath, CancellationToken token = default)
|
||||||
|
124
AsbCloudInfrastructure/Services/GoogleDriveService.cs
Normal file
124
AsbCloudInfrastructure/Services/GoogleDriveService.cs
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
using Google.Apis.Auth.OAuth2;
|
||||||
|
using Google.Apis.Auth.OAuth2.Flows;
|
||||||
|
using Google.Apis.Auth.OAuth2.Responses;
|
||||||
|
using Google.Apis.Drive.v3;
|
||||||
|
using Google.Apis.Services;
|
||||||
|
using Google.Apis.Util.Store;
|
||||||
|
using Google.Apis.Drive.v3.Data;
|
||||||
|
using System.IO;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using AsbCloudApp.Services;
|
||||||
|
|
||||||
|
namespace AsbCloudInfrastructure.Services
|
||||||
|
{
|
||||||
|
public class GoogleDriveService : IGoogleDriveService
|
||||||
|
{
|
||||||
|
private readonly DriveService service;
|
||||||
|
|
||||||
|
public GoogleDriveService()
|
||||||
|
{ // ключи для почты asbautodrilling@gmail.com.
|
||||||
|
var tokenResponse = new TokenResponse
|
||||||
|
{
|
||||||
|
AccessToken = "ya29.a0ARrdaM-lM7q0TIC_DXixR4oW63QUftjSPHl-8nIdvZwtqA8Z1bXtlYpDrQXj9UFTjW8FW8uqPMrdamUSp4kO4a9JX7FddkBWxaJ_omSJpqzDfnHTHA_7-zGMUohaAsmPLsQtFz_GUmB5ZoVLmA8xWdbJxVxU",
|
||||||
|
RefreshToken = "1//04FeDguc19IfgCgYIARAAGAQSNwF-L9Ir8U7wX2seanUzsxXXGgFzOYQqjbtN9O27ZZybbOobZjVAo_4_eFNLMX1ElPKOFVWsrJQ"
|
||||||
|
};
|
||||||
|
|
||||||
|
var applicationName = "Files"; // Use the name of the project in Google Cloud
|
||||||
|
var username = "asbautodrilling@gmail.com"; // Use your email
|
||||||
|
|
||||||
|
var apiCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
|
||||||
|
{
|
||||||
|
ClientSecrets = new ClientSecrets
|
||||||
|
{
|
||||||
|
ClientId = "1020584579240-f7amqg35qg7j94ta1ntgitajq27cgh49.apps.googleusercontent.com",
|
||||||
|
ClientSecret = "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"
|
||||||
|
},
|
||||||
|
Scopes = new[] {DriveService.Scope.Drive},
|
||||||
|
DataStore = new FileDataStore(applicationName)
|
||||||
|
});
|
||||||
|
|
||||||
|
var credential = new UserCredential(apiCodeFlow, username, tokenResponse);
|
||||||
|
|
||||||
|
var newService = new DriveService(new BaseClientService.Initializer
|
||||||
|
{
|
||||||
|
HttpClientInitializer = credential,
|
||||||
|
ApplicationName = applicationName
|
||||||
|
});
|
||||||
|
this.service = newService;
|
||||||
|
}
|
||||||
|
|
||||||
|
// public IEnumerable<Google.Apis.Drive.v3.Data.File> GetAllFiles() // get file names
|
||||||
|
// {
|
||||||
|
// var fileList = service.Files.List();
|
||||||
|
// fileList.Fields = "files(id, webViewLink, size)";
|
||||||
|
// //fileList.Q =$"mimeType!='application/vnd.google-apps.folder' and '{folder}' in parents";
|
||||||
|
// //fileList.Fields = "nextPageToken, files(id, name, size, mimeType)";
|
||||||
|
//
|
||||||
|
// var result = new List<Google.Apis.Drive.v3.Data.File>();
|
||||||
|
// string pageToken = null;
|
||||||
|
// do
|
||||||
|
// {
|
||||||
|
// fileList.PageToken = pageToken;
|
||||||
|
// var filesResult = fileList.Execute();
|
||||||
|
// var files = filesResult.Files;
|
||||||
|
// pageToken = filesResult.NextPageToken;
|
||||||
|
// result.AddRange(files);
|
||||||
|
// } while (pageToken != null);
|
||||||
|
//
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
|
||||||
|
public string GetFileWebLink(string idFile)
|
||||||
|
{
|
||||||
|
var fileList = service.Files.List();
|
||||||
|
fileList.Fields = "files(id, webViewLink, size)";
|
||||||
|
var filesResult = fileList.Execute();
|
||||||
|
var file = filesResult.Files.FirstOrDefault(f => f.Id == idFile);
|
||||||
|
return file?.WebViewLink ?? string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
// У Гугла почему-то folder это и папка, и файл.
|
||||||
|
public string CreateFolder(string folderName)
|
||||||
|
{
|
||||||
|
var driveFolder = new Google.Apis.Drive.v3.Data.File();
|
||||||
|
driveFolder.Name = folderName;
|
||||||
|
driveFolder.MimeType = "application/vnd.google-apps.folder";
|
||||||
|
//driveFolder.Parents = new string[] { parent };
|
||||||
|
var command = service.Files.Create(driveFolder);
|
||||||
|
var file = command.Execute();
|
||||||
|
return file.Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CreatePublicPermissionForFile(string idFile)
|
||||||
|
{
|
||||||
|
var permission = new Permission() { Type = "anyone", Role = "reader"};
|
||||||
|
var addPermissionRequest = service.Permissions.Create(permission, idFile);
|
||||||
|
addPermissionRequest.Execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
public string UploadFile(Stream file, string fileName, string fileMime, string fileDescription)
|
||||||
|
{
|
||||||
|
var driveFile = new Google.Apis.Drive.v3.Data.File();
|
||||||
|
driveFile.Name = fileName;
|
||||||
|
driveFile.Description = fileDescription;
|
||||||
|
driveFile.MimeType = fileMime;
|
||||||
|
//driveFile.Parents = new [] {folder};
|
||||||
|
|
||||||
|
var request = service.Files.Create(driveFile, file, fileMime);
|
||||||
|
request.Fields = "id";
|
||||||
|
|
||||||
|
var response = request.Upload();
|
||||||
|
if (response.Status != Google.Apis.Upload.UploadStatus.Completed)
|
||||||
|
throw response.Exception;
|
||||||
|
|
||||||
|
return request.ResponseBody.Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteFile(string fileId)
|
||||||
|
{
|
||||||
|
var command = service.Files.Delete(fileId);
|
||||||
|
var result = command.Execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -59,7 +59,7 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
};
|
};
|
||||||
generator.Make(reportFileName);
|
generator.Make(reportFileName);
|
||||||
|
|
||||||
var fileService = new FileService(context);
|
var fileService = new FileService(context, new GoogleDriveService());
|
||||||
var fileInfo = fileService.MoveAsync(idWell, idUser, ReportCategoryId, reportFileName, reportFileName).Result;
|
var fileInfo = fileService.MoveAsync(idWell, idUser, ReportCategoryId, reportFileName, reportFileName).Result;
|
||||||
|
|
||||||
progressHandler.Invoke(new
|
progressHandler.Invoke(new
|
||||||
|
@ -30,7 +30,24 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
if (fileInfo is null)
|
if (fileInfo is null)
|
||||||
return NoContent();
|
return NoContent();
|
||||||
var relativePath = fileService.GetUrl(fileInfo);
|
var relativePath = fileService.GetUrl(fileInfo);
|
||||||
|
|
||||||
return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Name);
|
return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("webLink")]
|
||||||
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
|
||||||
|
public async Task<IActionResult> GetWebLinkAsync(int idWell, CancellationToken token = default)
|
||||||
|
{
|
||||||
|
var fileInfo = await drillingProgramService.GetAsync(idWell, token)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
if (fileInfo is null)
|
||||||
|
return NoContent();
|
||||||
|
var relativePath = fileService.GetUrl(fileInfo);
|
||||||
|
|
||||||
|
var fileWebLink = fileService.CreateGoogleDriveWebLink(relativePath, fileInfo.Name);
|
||||||
|
await fileService.SaveWeblinkToFileInfo(fileInfo.Id, fileWebLink, token);
|
||||||
|
|
||||||
|
return Ok(fileWebLink);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,9 +12,11 @@ using System.Linq;
|
|||||||
// usage example at the very bottom
|
// usage example at the very bottom
|
||||||
namespace ConsoleApp1
|
namespace ConsoleApp1
|
||||||
{
|
{
|
||||||
public class GoogleDriveFilesService
|
public class GoogleDriveService
|
||||||
{
|
{
|
||||||
public DriveService GetService()
|
private readonly DriveService service;
|
||||||
|
|
||||||
|
public GoogleDriveService()
|
||||||
{ // ключи для почты asbautodrilling@gmail.com.
|
{ // ключи для почты asbautodrilling@gmail.com.
|
||||||
var tokenResponse = new TokenResponse
|
var tokenResponse = new TokenResponse
|
||||||
{
|
{
|
||||||
@ -38,17 +40,47 @@ namespace ConsoleApp1
|
|||||||
|
|
||||||
var credential = new UserCredential(apiCodeFlow, username, tokenResponse);
|
var credential = new UserCredential(apiCodeFlow, username, tokenResponse);
|
||||||
|
|
||||||
var service = new DriveService(new BaseClientService.Initializer
|
var newService = new DriveService(new BaseClientService.Initializer
|
||||||
{
|
{
|
||||||
HttpClientInitializer = credential,
|
HttpClientInitializer = credential,
|
||||||
ApplicationName = applicationName
|
ApplicationName = applicationName
|
||||||
});
|
});
|
||||||
return service;
|
this.service = newService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// public IEnumerable<Google.Apis.Drive.v3.Data.File> GetAllFiles() // get file names
|
||||||
|
// {
|
||||||
|
// var fileList = service.Files.List();
|
||||||
|
// fileList.Fields = "files(id, webViewLink, size)";
|
||||||
|
// //fileList.Q =$"mimeType!='application/vnd.google-apps.folder' and '{folder}' in parents";
|
||||||
|
// //fileList.Fields = "nextPageToken, files(id, name, size, mimeType)";
|
||||||
|
//
|
||||||
|
// var result = new List<Google.Apis.Drive.v3.Data.File>();
|
||||||
|
// string pageToken = null;
|
||||||
|
// do
|
||||||
|
// {
|
||||||
|
// fileList.PageToken = pageToken;
|
||||||
|
// var filesResult = fileList.Execute();
|
||||||
|
// var files = filesResult.Files;
|
||||||
|
// pageToken = filesResult.NextPageToken;
|
||||||
|
// result.AddRange(files);
|
||||||
|
// } while (pageToken != null);
|
||||||
|
//
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
|
||||||
public string CreateFolder(string folderName) // Это fileName. У гугла почему-то все называется folder: и папка, и файл.
|
public string GetFileWebLink(string idFile)
|
||||||
{ // С parent почему-то не дружит, выкидывает Exception.
|
{
|
||||||
var service = GetService();
|
var fileList = service.Files.List();
|
||||||
|
fileList.Fields = "files(id, webViewLink, size)";
|
||||||
|
var filesResult = fileList.Execute();
|
||||||
|
var file = filesResult.Files.FirstOrDefault(f => f.Id == idFile);
|
||||||
|
return file?.WebViewLink ?? string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
// У Гугла почему-то folder это и папка, и файл.
|
||||||
|
public string CreateFolder(string folderName)
|
||||||
|
{
|
||||||
var driveFolder = new Google.Apis.Drive.v3.Data.File();
|
var driveFolder = new Google.Apis.Drive.v3.Data.File();
|
||||||
driveFolder.Name = folderName;
|
driveFolder.Name = folderName;
|
||||||
driveFolder.MimeType = "application/vnd.google-apps.folder";
|
driveFolder.MimeType = "application/vnd.google-apps.folder";
|
||||||
@ -57,11 +89,16 @@ namespace ConsoleApp1
|
|||||||
var file = command.Execute();
|
var file = command.Execute();
|
||||||
return file.Id;
|
return file.Id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void CreatePublicPermissionForFile(string idFile)
|
||||||
|
{
|
||||||
|
var permission = new Permission() { Type = "anyone", Role = "reader"};
|
||||||
|
var addPermissionRequest = service.Permissions.Create(permission, idFile);
|
||||||
|
addPermissionRequest.Execute();
|
||||||
|
}
|
||||||
|
|
||||||
public string UploadFile(Stream file, string fileName, string fileMime, string fileDescription)
|
public string UploadFile(Stream file, string fileName, string fileMime, string fileDescription)
|
||||||
{
|
{
|
||||||
DriveService service = GetService();
|
|
||||||
|
|
||||||
var driveFile = new Google.Apis.Drive.v3.Data.File();
|
var driveFile = new Google.Apis.Drive.v3.Data.File();
|
||||||
driveFile.Name = fileName;
|
driveFile.Name = fileName;
|
||||||
driveFile.Description = fileDescription;
|
driveFile.Description = fileDescription;
|
||||||
@ -77,42 +114,9 @@ namespace ConsoleApp1
|
|||||||
|
|
||||||
return request.ResponseBody.Id;
|
return request.ResponseBody.Id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Google.Apis.Drive.v3.Data.File> GetAllFiles()
|
|
||||||
{
|
|
||||||
var service = GetService();
|
|
||||||
|
|
||||||
var fileList = service.Files.List();
|
|
||||||
fileList.Fields = "files(id, webViewLink, size)";
|
|
||||||
//fileList.Q =$"mimeType!='application/vnd.google-apps.folder' and '{folder}' in parents";
|
|
||||||
//fileList.Fields = "nextPageToken, files(id, name, size, mimeType)";
|
|
||||||
|
|
||||||
var result = new List<Google.Apis.Drive.v3.Data.File>();
|
|
||||||
string pageToken = null;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
fileList.PageToken = pageToken;
|
|
||||||
var filesResult = fileList.Execute();
|
|
||||||
var files = filesResult.Files;
|
|
||||||
pageToken = filesResult.NextPageToken;
|
|
||||||
result.AddRange(files);
|
|
||||||
} while (pageToken != null);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Google.Apis.Drive.v3.Data.File GetFileById(string idFile)
|
|
||||||
{
|
|
||||||
var service = GetService();
|
|
||||||
var fileList = service.Files.List();
|
|
||||||
fileList.Fields = "files(id, webViewLink, size)";
|
|
||||||
var filesResult = fileList.Execute();
|
|
||||||
return filesResult.Files.FirstOrDefault(f => f.Id == idFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DeleteFile(string fileId)
|
public void DeleteFile(string fileId)
|
||||||
{
|
{
|
||||||
var service = GetService();
|
|
||||||
var command = service.Files.Delete(fileId);
|
var command = service.Files.Delete(fileId);
|
||||||
var result = command.Execute();
|
var result = command.Execute();
|
||||||
}
|
}
|
||||||
@ -121,10 +125,10 @@ namespace ConsoleApp1
|
|||||||
|
|
||||||
// usage example:
|
// usage example:
|
||||||
|
|
||||||
// var serviceWrapper = new GoogleDriveFilesService();
|
// var service = new GoogleDriveService();
|
||||||
//
|
//
|
||||||
// //var files = serviceWrapper.GetAllFiles();
|
// //var files = serviceWrapper.GetAllFiles();
|
||||||
// var service = serviceWrapper.GetService();
|
//
|
||||||
// // foreach (var file in files)
|
// // foreach (var file in files)
|
||||||
// // {
|
// // {
|
||||||
// // var permission = new Permission() { Type = "anyone", Role = "reader"};
|
// // var permission = new Permission() { Type = "anyone", Role = "reader"};
|
||||||
@ -136,9 +140,7 @@ namespace ConsoleApp1
|
|||||||
// var path = "/home/cult/First.xlsx";
|
// var path = "/home/cult/First.xlsx";
|
||||||
// var fileInfo = new FileInfo(path);
|
// var fileInfo = new FileInfo(path);
|
||||||
// var fileStream = System.IO.File.Open(path, FileMode.Open);
|
// var fileStream = System.IO.File.Open(path, FileMode.Open);
|
||||||
// var uploadedFileId = serviceWrapper.UploadFile(fileStream, fileInfo.Name, "", "uploaded");
|
// var uploadedFileId = service.UploadFile(fileStream, fileInfo.Name, "", "uploaded");
|
||||||
// var permission = new Permission() { Type = "anyone", Role = "reader"};
|
// service.CreatePublicPermissionForFile(uploadedFileId);
|
||||||
// var addPermissionRequest = service.Permissions.Create(permission, uploadedFileId);
|
// var webLink = service.GetFileWebLink(uploadedFileId);
|
||||||
// addPermissionRequest.Execute();
|
// Console.WriteLine(webLink);
|
||||||
// var file = serviceWrapper.GetFileById(uploadedFileId);
|
|
||||||
// Console.WriteLine(file.WebViewLink);
|
|
@ -15,10 +15,10 @@ namespace ConsoleApp1
|
|||||||
{
|
{
|
||||||
static void Main(/*string[] args*/)
|
static void Main(/*string[] args*/)
|
||||||
{
|
{
|
||||||
var serviceWrapper = new GoogleDriveFilesService();
|
var service = new GoogleDriveService();
|
||||||
|
|
||||||
//var files = serviceWrapper.GetAllFiles();
|
//var files = serviceWrapper.GetAllFiles();
|
||||||
var service = serviceWrapper.GetService();
|
|
||||||
// foreach (var file in files)
|
// foreach (var file in files)
|
||||||
// {
|
// {
|
||||||
// var permission = new Permission() { Type = "anyone", Role = "reader"};
|
// var permission = new Permission() { Type = "anyone", Role = "reader"};
|
||||||
@ -30,12 +30,10 @@ namespace ConsoleApp1
|
|||||||
var path = "/home/cult/First.xlsx";
|
var path = "/home/cult/First.xlsx";
|
||||||
var fileInfo = new FileInfo(path);
|
var fileInfo = new FileInfo(path);
|
||||||
var fileStream = System.IO.File.Open(path, FileMode.Open);
|
var fileStream = System.IO.File.Open(path, FileMode.Open);
|
||||||
var uploadedFileId = serviceWrapper.UploadFile(fileStream, fileInfo.Name, "", "uploaded");
|
var uploadedFileId = service.UploadFile(fileStream, fileInfo.Name, "", "uploaded");
|
||||||
var permission = new Permission() { Type = "anyone", Role = "reader"};
|
service.CreatePublicPermissionForFile(uploadedFileId);
|
||||||
var addPermissionRequest = service.Permissions.Create(permission, uploadedFileId);
|
var webLink = service.GetFileWebLink(uploadedFileId);
|
||||||
addPermissionRequest.Execute();
|
Console.WriteLine(webLink);
|
||||||
var file = serviceWrapper.GetFileById(uploadedFileId);
|
|
||||||
Console.WriteLine(file.WebViewLink);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user