Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

6.6 KB
Raw Blame
import { Ionicons } from '@expo/vector-icons';
import * as DocumentPicker from 'expo-document-picker';
import { File, Paths } from 'expo-file-system';
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { View, StyleSheet, TouchableOpacity } from 'react-native';

import { ThemedText } from '@/components/themed/ThemedText';
import { ThemedView } from '@/components/themed/ThemedView';
import { useColors } from '@/hooks/useColorScheme';
import { getFileForFilename } from '@/utils/FileUtility';
import type { Attachment } from '@aliasvault/models/vault';

type AttachmentUploaderProps = {
  /** The attachments without their bytes, which never go into the list. */
  attachments: Omit<Attachment, 'Blob'>[];
  onAttachmentsChange: (attachments: Omit<Attachment, 'Blob'>[]) => void;
  /** Receives the bytes of each picked file. */
  onAttachmentRead: (attachmentId: string, bytes: Uint8Array) => void;
}

/**
 * This component allows uploading and managing attachments for a credential.
 * Deletion is pending until the item is saved - attachments are removed from the list
 * immediately but only persisted when the parent form saves.
 */
export const AttachmentUploader: React.FC<AttachmentUploaderProps> = ({
  attachments,
  onAttachmentsChange,
  onAttachmentRead,
}) => {
  const { t } = useTranslation();
  const colors = useColors();
  const [statusMessage, setStatusMessage] = useState<string>('');

  /**
   * Handles file selection and upload.
   */
  const handleFileSelection = async (): Promise<void> => {
    try {
      const result = await DocumentPicker.getDocumentAsync({
        multiple: true,
        copyToCacheDirectory: true,
      });

      if (result.canceled) {
        return;
      }

      const newAttachments = [...attachments];

      for (const file of result.assets) {
        if (file.uri) {
          try {
            let sourceFile = new File(file.uri);
            let tempFile: File | null = null;

            /*
             * If the URI is a content:// URI and copyToCacheDirectory didn't work,
             * try to copy it to a readable location
             */
            if (file.uri.startsWith('content://')) {
              tempFile = getFileForFilename(Paths.cache, file.name);
              if (tempFile.exists) {
                tempFile.delete();
              }
              await sourceFile.copy(tempFile);
              sourceFile = tempFile;
            }

            const attachmentId = crypto.randomUUID();
            onAttachmentRead(attachmentId, await sourceFile.bytes());

            const attachment: Omit<Attachment, 'Blob'> = {
              Id: attachmentId,
              Filename: file.name,
              ItemId: '', // Will be set when saving item
              CreatedAt: new Date().toISOString(),
              UpdatedAt: new Date().toISOString(),
              IsDeleted: false,
            };

            newAttachments.push(attachment);

            // Clean up temporary file if we created one
            if (tempFile?.exists) {
              try {
                tempFile.delete();
              } catch (cleanupError) {
                console.warn('Failed to cleanup temporary file:', cleanupError);
              }
            }
          } catch (fileError) {
            console.error('Error reading file:', fileError);
            setStatusMessage(t('attachmentUploader.uploadError', { error: file.name }));
            setTimeout(() => setStatusMessage(''), 3000);
          }
        }
      }

      onAttachmentsChange(newAttachments);
      setStatusMessage('');
    } catch (error) {
      console.error('Error uploading files:', error);
      setStatusMessage(t('attachmentUploader.uploadError', { error: error instanceof Error ? error.message : String(error) }));
      setTimeout(() => setStatusMessage(''), 3000);
    }
  };

  /**
   * Deletes an attachment immediately from the list (pending save).
   * The actual database deletion happens when the parent item is saved.
   */
  const deleteAttachment = (attachmentToDelete: Omit<Attachment, 'Blob'>): void => {
    const updatedAttachments = attachments.filter(a => a.Id !== attachmentToDelete.Id);
    onAttachmentsChange(updatedAttachments);
  };

  const activeAttachments = attachments.filter(a => !a.IsDeleted);

  const styles = StyleSheet.create({
    addButton: {
      alignItems: 'center',
      backgroundColor: colors.primary,
      borderRadius: 8,
      height: 35,
      justifyContent: 'center',
      marginTop: 12,
      width: '100%',
    },
    attachmentDate: {
      fontSize: 12,
    },
    attachmentInfo: {
      flex: 1,
    },
    attachmentItem: {
      alignItems: 'center',
      backgroundColor: colors.accentBackground,
      borderRadius: 8,
      flexDirection: 'row',
      justifyContent: 'space-between',
      marginBottom: 8,
      padding: 12,
    },
    attachmentName: {
      fontSize: 14,
      fontWeight: '500',
      marginBottom: 2,
    },
    container: {
      backgroundColor: colors.accentBackground,
    },
    deleteButton: {
      backgroundColor: colors.errorBackground,
      borderRadius: 6,
      marginLeft: 12,
      padding: 8,
    },
    deleteButtonText: {
      color: colors.text,
      fontSize: 12,
      fontWeight: '600',
    },
    statusMessage: {
      fontSize: 14,
      textAlign: 'center',
    },
  });

  return (
    <ThemedView style={styles.container}>
      {statusMessage && (
        <ThemedText style={styles.statusMessage} type="subtitle">
          {statusMessage}
        </ThemedText>
      )}

      {activeAttachments.length > 0 && (
        <View>
          {activeAttachments.map(attachment => (
            <View key={attachment.Id} style={styles.attachmentItem}>
              <View style={styles.attachmentInfo}>
                <ThemedText style={styles.attachmentName}>
                  {attachment.Filename}
                </ThemedText>
                <ThemedText style={styles.attachmentDate} type="subtitle">
                  {new Date(attachment.CreatedAt).toLocaleDateString()}
                </ThemedText>
              </View>
              <TouchableOpacity
                style={styles.deleteButton}
                onPress={() => deleteAttachment(attachment)}
              >
                <ThemedText style={styles.deleteButtonText}>
                  {t('common.delete')}
                </ThemedText>
              </TouchableOpacity>
            </View>
          ))}
        </View>
      )}

      <TouchableOpacity
        style={styles.addButton}
        onPress={handleFileSelection}
      >
        <Ionicons name="add" size={24} color={colors.background} />
      </TouchableOpacity>
    </ThemedView>
  );
};