Pre-beta: picture storage should be more responsive if on disk

This commit is contained in:
Robert McRackan 2019-11-13 11:11:00 -05:00
parent e69df2abbc
commit 47360c036d
2 changed files with 46 additions and 51 deletions

View File

@ -42,7 +42,13 @@ namespace FileManager
public static (bool isDefault, byte[] bytes) GetPicture(PictureDefinition def)
{
if (!cache.ContainsKey(def))
cache.Add(def, null);
{
var path = getPath(def);
cache[def]
= FileUtility.FileExists(path)
? File.ReadAllBytes(path)
: null;
}
return (cache[def] == null, cache[def] ?? getDefaultImage(def.Size));
}
@ -64,33 +70,19 @@ namespace FileManager
return;
isProcessing = true;
PictureDefinition def;
string path;
while (true)
{
def = cache
var def = cache
.Where(kvp => kvp.Value is null)
.Select(kvp => kvp.Key)
// 80x80 should be 1st since it's enum value == 0
.OrderBy(d => d.PictureId)
.FirstOrDefault();
// no more null entries. all requsted images are cached
if (string.IsNullOrWhiteSpace(def.PictureId))
return;
path = getPath(def);
// we found the next one to download
if (!FileUtility.FileExists(path))
break;
// file exists. read into cache. try again
// the point is to throttle web calls. therefore only return if we performed a d/l or there are no null cache entries
cache[def] = File.ReadAllBytes(path);
}
var bytes = download(def);
File.WriteAllBytes(path, bytes);
var bytes = downloadBytes(def);
saveFile(def, bytes);
cache[def] = bytes;
}
finally
@ -98,12 +90,18 @@ namespace FileManager
isProcessing = false;
}
}
private static HttpClient imageDownloadClient { get; } = new HttpClient();
private static byte[] download(PictureDefinition def)
private static byte[] downloadBytes(PictureDefinition def)
{
var sz = def.Size.ToString().Split('x')[1];
var bytes = imageDownloadClient.GetByteArrayAsync("ht" + $"tps://images-na.ssl-images-amazon.com/images/I/{def.PictureId}._SL{sz}_.jpg").Result;
return bytes;
return imageDownloadClient.GetByteArrayAsync("ht" + $"tps://images-na.ssl-images-amazon.com/images/I/{def.PictureId}._SL{sz}_.jpg").Result;
}
private static void saveFile(PictureDefinition def, byte[] bytes)
{
var path = getPath(def);
File.WriteAllBytes(path, bytes);
}
}
}

View File

@ -1,30 +1,27 @@
-- begin BETA ---------------------------------------------------------------------------------------------------------------------
throttle needed for downloading pdf.s
UI
- download now incl pdf
- update wording in menu
pdf menu should be 'PDFs only'
README
- download now incl pdf
- update readme
OLD WEB DOWNLOADER
- download logic in DownloadPdf should look more like DownloadBook
picture storage should be more responsive if on disk?
TESTING
possible bug: no mdf,ldf files created in C:\Users\[username]
download now incl pdf
pdf menu should be 'PDFs only'
update wording in menu
update readme
no mdf,ldf files created in C:\Users\[username]
Warn of known performance issues
- Library import
- Tag add/edit
- Grid is slow to respond loading when books aren't liberated
- get decrypt key -- unavoidable
Create release version and installer
https://dotnetcoretutorials.com/2019/06/20/publishing-a-single-exe-file-in-net-core-3-0/
RELEASE TO BETA
- Create release version and installer
https://dotnetcoretutorials.com/2019/06/20/publishing-a-single-exe-file-in-net-core-3-0/
- Warn of known performance issues
- Library import
- Tag add/edit
- Grid is slow to respond loading when books aren't liberated
- get decrypt key -- unavoidable
-- end BETA ---------------------------------------------------------------------------------------------------------------------
-- begin ENHANCEMENT, IMPORT UI ---------------------------------------------------------------------------------------------------------------------