Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/app/core/services/interfaces/localfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @license
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {InjectionToken} from '@angular/core';

export const LOCAL_FILE_SERVICE = new InjectionToken<LocalFileService>(
'LocalFileService',
);

/**
* Service to provide methods to handle local files.
*/
export abstract class LocalFileService {
abstract createMessagePartFromFile(file: File): Promise<any>;
}
60 changes: 60 additions & 0 deletions src/app/core/services/local-file.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @license
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {TestBed} from '@angular/core/testing';

import {LocalFileServiceImpl} from './local-file.service';

describe('LocalFileServiceImpl', () => {
let service: LocalFileServiceImpl;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [LocalFileServiceImpl],
});
service = TestBed.inject(LocalFileServiceImpl);
});

it('createMessagePartFromFile successfully reads a file and returns a message part',
async () => {
const fileContent = 'Hello World';
const fileContentBase64 = btoa(fileContent); // Base64 for "Hello World"
const dataUrl = `data:text/plain;base64,${fileContentBase64}`;
const mockFile = new File([fileContent], 'test.txt', {
type: 'text/plain',
});
const mockFileReader = jasmine.createSpyObj('FileReader', [
'readAsDataURL',
'onload',
]);
spyOn(window, 'FileReader').and.returnValue(mockFileReader);
mockFileReader.result = dataUrl;
mockFileReader.readAsDataURL.and.callFake(() => {
mockFileReader.onload({target: mockFileReader});
});

const result = await service.createMessagePartFromFile(mockFile);

expect(mockFileReader.readAsDataURL).toHaveBeenCalledWith(mockFile);
expect(result).toEqual({
inlineData: {
displayName: 'test.txt',
data: fileContentBase64,
mimeType: 'text/plain',
},
});
});
});
50 changes: 50 additions & 0 deletions src/app/core/services/local-file.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @license
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {Injectable} from '@angular/core';

import {LocalFileService} from './interfaces/localfile';

/**
* Service to provide methods to handle local files.
*/
@Injectable({
providedIn: 'root',
})
export class LocalFileServiceImpl extends LocalFileService {
async createMessagePartFromFile(file: File): Promise<any> {
return {
inlineData: {
displayName: file.name,
data: await this.readFileAsBytes(file),
mimeType: file.type,
},
};
}

private readFileAsBytes(file: File): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e: any) => {
const base64Data = e.target.result.split(',')[1];
resolve(base64Data);
};
reader.onerror = reject;
reader.readAsDataURL(file); // Read as raw bytes
});
}
}
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ import {EVAL_SERVICE, EvalService} from './app/core/services/eval.service';
import {EVENT_SERVICE, EventService} from './app/core/services/event.service';
import {FEATURE_FLAG_SERVICE, FeatureFlagService} from './app/core/services/feature-flag.service';
import {GRAPH_SERVICE, GraphService} from './app/core/services/graph.service';
import {LOCAL_FILE_SERVICE} from './app/core/services/interfaces/localfile';
import {SAFE_VALUES_SERVICE} from './app/core/services/interfaces/safevalues';
import {STRING_TO_COLOR_SERVICE, StringToColorService} from './app/core/services/interfaces/string-to-color';
import {LocalFileServiceImpl} from './app/core/services/local-file.service';
import {SafeValuesServiceImpl} from './app/core/services/safevalues.service';
import {SESSION_SERVICE, SessionService} from './app/core/services/session.service';
import {STREAM_CHAT_SERVICE, StreamChatService} from './app/core/services/stream-chat.service';
Expand Down Expand Up @@ -78,6 +80,7 @@ fetch('./assets/config/runtime-config.json')
{provide: GRAPH_SERVICE, useClass: GraphService},
{provide: STRING_TO_COLOR_SERVICE, useClass: StringToColorServiceImpl},
{provide: SAFE_VALUES_SERVICE, useClass: SafeValuesServiceImpl},
{provide: LOCAL_FILE_SERVICE, useClass: LocalFileServiceImpl},
{provide: MARKDOWN_COMPONENT, useValue: MarkdownComponent},
...(config.logo ?
[{provide: LOGO_COMPONENT, useValue: CustomLogoComponent}] :
Expand Down