Skip to content

Commit

Permalink
Fix: webnative directory lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
avivash committed Feb 21, 2023
1 parent d81d6e5 commit 1965be3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
36 changes: 23 additions & 13 deletions src/lib/account-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ interface AvatarFile extends PuttableUnixTree, WNFile {
};
}

export const ACCOUNT_SETTINGS_DIR = ["private", "settings"];
const AVATAR_DIR = [...ACCOUNT_SETTINGS_DIR, "avatars"];
const AVATAR_ARCHIVE_DIR = [...AVATAR_DIR, "archive"];
export const ACCOUNT_SETTINGS_DIR = wn.path.directory("private", "settings");
const AVATAR_DIR = wn.path.combine(
ACCOUNT_SETTINGS_DIR,
wn.path.directory("avatars")
);
const AVATAR_ARCHIVE_DIR = wn.path.combine(
AVATAR_DIR,
wn.path.directory("archive")
);
const AVATAR_FILE_NAME = "avatar";
const FILE_SIZE_LIMIT = 20;

Expand All @@ -43,15 +49,15 @@ const FILE_SIZE_LIMIT = 20;
*/
const archiveOldAvatar = async (): Promise<void> => {
const fs = getRecoil(filesystemStore);

// Return if user has not uploaded an avatar yet
const avatarDirExists = await fs.exists(wn.path.file(...AVATAR_DIR));
const avatarDirExists = await fs.exists(AVATAR_DIR);
if (!avatarDirExists) {
return;
}

// Find the filename of the old avatar
const path = wn.path.directory(...AVATAR_DIR);
const links = await fs.ls(path);
const links = await fs.ls(AVATAR_DIR);
const oldAvatarFileName = Object.keys(links).find((key) =>
key.includes(AVATAR_FILE_NAME)
);
Expand All @@ -61,8 +67,11 @@ const archiveOldAvatar = async (): Promise<void> => {
}`;

// Move old avatar to archive dir
const fromPath = wn.path.file(...AVATAR_DIR, oldAvatarFileName);
const toPath = wn.path.file(...AVATAR_ARCHIVE_DIR, archiveFileName);
const fromPath = wn.path.combine(AVATAR_DIR, wn.path.file(oldAvatarFileName));
const toPath = wn.path.combine(
AVATAR_ARCHIVE_DIR,
wn.path.file(archiveFileName)
);
await fs.mv(fromPath, toPath);

// Announce the changes to the server
Expand All @@ -82,15 +91,14 @@ export const getAvatarFromWNFS = async (): Promise<void> => {
setRecoil(accountSettingsStore, { ...accountSettings, loading: true });

// If the avatar dir doesn't exist, silently fail and let the UI handle it
const avatarDirExists = await fs.exists(wn.path.file(...AVATAR_DIR));
const avatarDirExists = await fs.exists(AVATAR_DIR);
if (!avatarDirExists) {
setRecoil(accountSettingsStore, { ...accountSettings, loading: false });
return;
}

// Find the file that matches the AVATAR_FILE_NAME
const path = wn.path.directory(...AVATAR_DIR);
const links = await fs.ls(path);
const links = await fs.ls(AVATAR_DIR);
const avatarName = Object.keys(links).find((key) =>
key.includes(AVATAR_FILE_NAME)
);
Expand All @@ -101,7 +109,9 @@ export const getAvatarFromWNFS = async (): Promise<void> => {
return;
}

const file = await fs.get(wn.path.file(...AVATAR_DIR, `${avatarName}`));
const file = await fs.get(
wn.path.combine(AVATAR_DIR, wn.path.file(`${avatarName}`))
);

// The CID for private files is currently located in `file.header.content`
const cid = (file as AvatarFile).header.content.toString();
Expand Down Expand Up @@ -167,7 +177,7 @@ export const uploadAvatarToWNFS = async (image: File): Promise<void> => {

// Create a sub directory and add the avatar
await fs.write(
wn.path.file(...AVATAR_DIR, updatedImage.name),
wn.path.combine(AVATAR_DIR, wn.path.file(updatedImage.name)),
await fileToUint8Array(updatedImage)
);

Expand Down
7 changes: 3 additions & 4 deletions src/lib/auth/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as uint8arrays from "uint8arrays";
import * as webnative from "webnative";
import type FileSystem from "webnative/fs/index";
import { sha256 } from "webnative/components/crypto/implementation/browser";
import { publicKeyToDid } from "webnative/did/transformers";
Expand Down Expand Up @@ -45,9 +44,9 @@ export const debouncedIsUsernameAvailable = asyncDebounce(
* @param fs FileSystem
*/
const initializeFilesystem = async (fs: FileSystem): Promise<void> => {
await fs.mkdir(webnative.path.directory(...GALLERY_DIRS[AREAS.PUBLIC]));
await fs.mkdir(webnative.path.directory(...GALLERY_DIRS[AREAS.PRIVATE]));
await fs.mkdir(webnative.path.directory(...ACCOUNT_SETTINGS_DIR));
await fs.mkdir(GALLERY_DIRS[AREAS.PUBLIC]);
await fs.mkdir(GALLERY_DIRS[AREAS.PRIVATE]);
await fs.mkdir(ACCOUNT_SETTINGS_DIR);
};

export const createDID = async (
Expand Down
22 changes: 11 additions & 11 deletions src/routes/gallery/lib/gallery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ type Link = {
size: number;
};

export const GALLERY_DIRS: {
[key: string]: string[];
} = {
[AREAS.PUBLIC]: ["public", "gallery"],
[AREAS.PRIVATE]: ["private", "gallery"],
export const GALLERY_DIRS = {
[AREAS.PUBLIC]: wn.path.directory("public", "gallery"),
[AREAS.PRIVATE]: wn.path.directory("private", "gallery"),
};

const FILE_SIZE_LIMIT = 20;
Expand All @@ -55,15 +53,15 @@ export const getImagesFromWNFS: () => Promise<void> = async () => {
const isPrivate = selectedArea === AREAS.PRIVATE;

// Set path to either private or public gallery dir
const path = wn.path.directory(...GALLERY_DIRS[selectedArea]);
const path = GALLERY_DIRS[selectedArea];

// Get list of links for files in the gallery dir
const links = await fs.ls(path);

let images = await Promise.all(
Object.entries(links).map(async ([name]) => {
const file = await fs.get(
wn.path.file(...GALLERY_DIRS[selectedArea], `${name}`)
wn.path.combine(GALLERY_DIRS[selectedArea], wn.path.file(`${name}`))
);

if (!isFile(file)) return null;
Expand Down Expand Up @@ -141,15 +139,15 @@ export const uploadImageToWNFS: (image: File) => Promise<void> = async (

// Reject the upload if the image already exists in the directory
const imageExists = await fs.exists(
wn.path.file(...GALLERY_DIRS[selectedArea], image.name)
wn.path.combine(GALLERY_DIRS[selectedArea], wn.path.file(image.name))
);
if (imageExists) {
throw new Error(`${image.name} image already exists`);
}

// Create a sub directory and add some content
await fs.write(
wn.path.file(...GALLERY_DIRS[selectedArea], image.name),
wn.path.combine(GALLERY_DIRS[selectedArea], wn.path.file(image.name)),
await fileToUint8Array(image)
);

Expand Down Expand Up @@ -181,12 +179,14 @@ export const deleteImageFromWNFS: (name: string) => Promise<void> = async (
const { selectedArea } = gallery;

const imageExists = await fs.exists(
wn.path.file(...GALLERY_DIRS[selectedArea], name)
wn.path.combine(GALLERY_DIRS[selectedArea], wn.path.file(name))
);

if (imageExists) {
// Remove images from server
await fs.rm(wn.path.file(...GALLERY_DIRS[selectedArea], name));
await fs.rm(
wn.path.combine(GALLERY_DIRS[selectedArea], wn.path.file(name))
);

// Announce the changes to the server
await fs.publish();
Expand Down

1 comment on commit 1965be3

@vercel
Copy link

@vercel vercel bot commented on 1965be3 Feb 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.