async function appSync() {
  try {
    /** Sync from remote updated first to get the latest state of synchronization
     * @TODO: can consider simply use aws s3 sync for this operation
     * @TODO: Trigger Lambda from Upload / Delete on the S3 to update the database
     */
    log.trace({ adminUser: ADMIN_USER_ID }, `appSync starts`);
    const hasSyncFrom = await syncFromRemote();
    if (!hasSyncFrom) throw new Error(`syncFromRemote return false`);

    observeFileChange(ROOT_PATH, {
      ignored: IGNORED_PATH_REGEXP,
      ignoreInitial: true
    }).subscribe({
      async next({ event, filePath }) {
        if (event === "ready") {
          log.info(
            { event, filePath },
            `File System Watch has began operation from root dir: ${ROOT_PATH}`
          );
        } else if (event === "add") {
          log.info(
            { event, filePath },
            `File System has detected ${event} event at ${filePath}`
          );
          /** Upload to S3 */
          const hasUpload = await uploadS3WithStream(S3_BUCKET_NAME, filePath);
          /** Scan / Save to DB = */
          if (hasUpload) {
            await scanFileToDB(filePath);
          } else {
            log.error({ filePath, hasUpload }, `File fails to upload`);
          }
        } else if (event === "change") {
          log.info(
            { event, filePath },
            `File System has detected ${event} event at ${filePath}`
          );
          /** Upload to S3 */
          const hasUpload = await uploadS3WithStream(S3_BUCKET_NAME, filePath);
          if (hasUpload) {
            log.info(
              { hasUpload },
              `${filePath} has uploaded to S3. now begin the updateDBFilesFromLocal`
            );
            await updateDBFilesFromLocal(filePath);
          } else {
            log.error({ filePath, hasUpload }, `File fails to upload to S3`);
          }
        } else if (event === "unlink") {
          log.info(
            { event, filePath },
            `File System has detected ${event} event at ${filePath}`
          );
          /** Delete object from S3 and update in DB */
          const s3FileKey = getS3FileKey(filePath);
          const deleteResponse = await deleteObjects(S3_BUCKET_NAME, [
            { Key: s3FileKey }
          ]);
          if (deleteResponse) {
            await deleteOneFileByPath(filePath);
          } else {
            log.error(
              { filePath, deleteResponse },
              `File fails to delete from S3`
            );
          }
        } else {
          throw new Error(`Unrecognise event value: ${event}`);
        }
      },
      error(err) {
        throw new Error(`Error occurs in observeFileCreation: ${err.message} `);
      }
    });
  } catch (err) {
    throw new Error(`Error occurs in controller appSync: ${err.message}`);
  }
}
/** appSync Watch files change , Scan to DB and Upload to S3 */
appSync Watch files change , Scan to DB and Upload to S3
appSync Watch files change Scan to DB and Upload to S3
async function appSync() {
  try {
    log.trace({ adminUser: ADMIN_USER_ID }, `appSync starts`);
    const hasSyncFrom = await syncFromRemote();
    if (!hasSyncFrom) throw new Error(`syncFromRemote return false`);
    observeFileChange(ROOT_PATH, {
      ignored: IGNORED_PATH_REGEXP,
      ignoreInitial: true
    }).subscribe({
      async next({ event, filePath }) {
        if (event === "ready") {
          log.info(
            { event, filePath },
            `File System Watch has began operation from root dir: ${ROOT_PATH}`
          );
        } else if (event === "add") {
          log.info(
            { event, filePath },
            `File System has detected ${event} event at ${filePath}`
          );
          const hasUpload = await uploadS3WithStream(S3_BUCKET_NAME, filePath);
          if (hasUpload) {
            await scanFileToDB(filePath);
          } else {
            log.error({ filePath, hasUpload }, `File fails to upload`);
          }
        } else if (event === "change") {
          log.info(
            { event, filePath },
            `File System has detected ${event} event at ${filePath}`
          );
          const hasUpload = await uploadS3WithStream(S3_BUCKET_NAME, filePath);
          if (hasUpload) {
            log.info(
              { hasUpload },
              `${filePath} has uploaded to S3. now begin the updateDBFilesFromLocal`
            );
            await updateDBFilesFromLocal(filePath);
          } else {
            log.error({ filePath, hasUpload }, `File fails to upload to S3`);
          }
        } else if (event === "unlink") {
          log.info(
            { event, filePath },
            `File System has detected ${event} event at ${filePath}`
          );
          const s3FileKey = getS3FileKey(filePath);
          const deleteResponse = await deleteObjects(S3_BUCKET_NAME, [
            { Key: s3FileKey }
          ]);
          if (deleteResponse) {
            await deleteOneFileByPath(filePath);
          } else {
            log.error(
              { filePath, deleteResponse },
              `File fails to delete from S3`
            );
          }
        } else {
          throw new Error(`Unrecognise event value: ${event}`);
        }
      },
      error(err) {
        throw new Error(`Error occurs in observeFileCreation: ${err.message} `);
      }
    });
  } catch (err) {
    throw new Error(`Error occurs in controller appSync: ${err.message}`);
  }
}
async function appSync ( ) { try { log . trace ( { adminUser : ADMIN_USER_ID } , ` ` ) ; const hasSyncFrom = await syncFromRemote ( ) ; if ( ! hasSyncFrom ) throw new Error ( ` ` ) ; observeFileChange ( ROOT_PATH , { ignored : IGNORED_PATH_REGEXP , ignoreInitial : true } ) . subscribe ( { async next ( { event , filePath } ) { if ( event === "ready" ) { log . info ( { event , filePath } , ` ${ ROOT_PATH } ` ) ; } else if ( event === "add" ) { log . info ( { event , filePath } , ` ${ event } ${ filePath } ` ) ; const hasUpload = await uploadS3WithStream ( S3_BUCKET_NAME , filePath ) ; if ( hasUpload ) { await scanFileToDB ( filePath ) ; } else { log . error ( { filePath , hasUpload } , ` ` ) ; } } else if ( event === "change" ) { log . info ( { event , filePath } , ` ${ event } ${ filePath } ` ) ; const hasUpload = await uploadS3WithStream ( S3_BUCKET_NAME , filePath ) ; if ( hasUpload ) { log . info ( { hasUpload } , ` ${ filePath } ` ) ; await updateDBFilesFromLocal ( filePath ) ; } else { log . error ( { filePath , hasUpload } , ` ` ) ; } } else if ( event === "unlink" ) { log . info ( { event , filePath } , ` ${ event } ${ filePath } ` ) ; const s3FileKey = getS3FileKey ( filePath ) ; const deleteResponse = await deleteObjects ( S3_BUCKET_NAME , [ { Key : s3FileKey } ] ) ; if ( deleteResponse ) { await deleteOneFileByPath ( filePath ) ; } else { log . error ( { filePath , deleteResponse } , ` ` ) ; } } else { throw new Error ( ` ${ event } ` ) ; } } , error ( err ) { throw new Error ( ` ${ err . message } ` ) ; } } ) ; } catch ( err ) { throw new Error ( ` ${ err . message } ` ) ; } }
appSync Watch files change , Scan to DB and Upload to S3
appSync Watch files change Scan to DB and Upload to S3
/** Sync from remote updated first to get the latest state of synchronization
     * @TODO: can consider simply use aws s3 sync for this operation
     * @TODO: Trigger Lambda from Upload / Delete on the S3 to update the database
     */ /** Upload to S3 */ /** Scan / Save to DB = */ /** Upload to S3 */ /** Delete object from S3 and update in DB */