Skip to content

Commit

Permalink
Add extension to the saved blob
Browse files Browse the repository at this point in the history
  • Loading branch information
kiennq committed Feb 3, 2024
1 parent 03bce61 commit 570b440
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
47 changes: 24 additions & 23 deletions csclip/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,44 +240,45 @@ async Task<string> GetDataToFileAsync(SaveDataToFileOptions options)

var stdFormat = CfToStandardFormat(options.cf);

try
if (data.Contains(stdFormat))
{
if (data.Contains(stdFormat))
if (options.store_path == null)
{
if (options.store_path == null)
{
return (string)await data.GetDataAsync(stdFormat); ;
}
else
return (string)await data.GetDataAsync(stdFormat); ;
}
else
{
var blob = await data.GetDataAsync(stdFormat);
if (blob is RandomAccessStreamReference)
{
var rawPayload = await data.GetDataAsync(stdFormat);
if (rawPayload is RandomAccessStreamReference)
var sblob = await (blob as RandomAccessStreamReference).OpenReadAsync();
var ext = MimeTypes.MimeTypeMap.GetExtension(sblob.ContentType);
var fileName = $"{Guid.NewGuid().ToString()}{ext}";
_ = Task.Run(async () =>
{
var fileName = Guid.NewGuid().ToString();
_ = Task.Run(async () =>
try
{
var sin = await (rawPayload as RandomAccessStreamReference).OpenReadAsync();
var tempFile = await StorageFile.CreateStreamedFileAsync("temp", async (sout) =>
{
await RandomAccessStream.CopyAndCloseAsync(sin, sout);
await RandomAccessStream.CopyAndCloseAsync(sblob, sout);
}, null);

var path = Path.GetFullPath(options.store_path);
var path = Path.GetFullPath($"{options.store_path}/");
(new FileInfo(path)).Directory.Create();
var folder = await StorageFolder.GetFolderFromPathAsync(path);
_ = tempFile.CopyAsync(folder, fileName, NameCollisionOption.ReplaceExisting);
});
await tempFile.CopyAsync(folder, fileName, NameCollisionOption.ReplaceExisting);
}
catch { }
});

return fileName;
}
else if (rawPayload is string)
{
return rawPayload as string;
}
return fileName;
}
else if (blob is string)
{
return blob as string;
}
}
}
catch {}

return "";
}
Expand Down
3 changes: 3 additions & 0 deletions csclip/csclip.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="MediaTypeMap.Core">
<Version>2.3.3</Version>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Threading">
<Version>17.8.14</Version>
</PackageReference>
Expand Down

0 comments on commit 570b440

Please sign in to comment.