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
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ describe('FilesTreeExplorerComponent', () => {

component.downloadFolder('/upload-folder');

expect(filesService.getFolderDownloadLink).toHaveBeenCalledWith('/upload-folder');
expect(filesService.getFolderDownloadLink).toHaveBeenCalledWith('/upload-folder', 'files');
expect(openSpy).toHaveBeenCalledWith('/upload-folder?zip=', '_blank');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { LoadingSpinnerComponent } from '@osf/shared/components/loading-spinner/
import { FileKind } from '@osf/shared/enums/file-kind.enum';
import { FileMenuType } from '@osf/shared/enums/file-menu-type.enum';
import { CurrentResourceType } from '@osf/shared/enums/resource-type.enum';
import { appendDownloadTrackingParams } from '@osf/shared/helpers/download-link.helper';
import { FileTreeMapper } from '@osf/shared/mappers/files/file-tree.mapper';
import { FilesMapper } from '@osf/shared/mappers/files/files.mapper';
import { FileModel } from '@osf/shared/models/files/file.model';
Expand Down Expand Up @@ -203,13 +204,13 @@ export class FilesTreeExplorerComponent {

downloadFile(link: string): void {
if (this.isBrowser) {
window.open(link)?.focus();
window.open(appendDownloadTrackingParams(link, 'files'))?.focus();
}
}

downloadFolder(downloadLink: string): void {
if (downloadLink) {
const link = this.filesService.getFolderDownloadLink(downloadLink);
const link = this.filesService.getFolderDownloadLink(downloadLink, 'files');
window.open(link, '_blank')?.focus();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,14 @@ describe('FileDetailComponent', () => {
setup();
(store.dispatch as Mock).mockClear();
const openSpy = vi.spyOn(window, 'open').mockReturnValue({ focus: vi.fn() } as unknown as Window);
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;

component.downloadRevision('3');

expect(dataciteService.logIdentifiableDownload).toHaveBeenCalledWith(component.fileMetadata$);
expect(openSpy).toHaveBeenCalledWith('https://osf.test/download/?revision=3');
expect(openSpy).toHaveBeenCalledWith(
`https://osf.test/download/?revision=3&source=file-detail&tz=${encodeURIComponent(timeZone)}`
);
expect(store.dispatch).toHaveBeenCalledWith(new GetFileRevisions('https://osf.test/upload'));
});

Expand Down Expand Up @@ -223,11 +226,14 @@ describe('FileDetailComponent', () => {
setup();
(store.dispatch as Mock).mockClear();
const openSpy = vi.spyOn(window, 'open').mockReturnValue({ focus: vi.fn() } as unknown as Window);
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;

component.downloadFile();

expect(dataciteService.logIdentifiableDownload).toHaveBeenCalledWith(component.fileMetadata$);
expect(openSpy).toHaveBeenCalledWith('https://osf.test/download');
expect(openSpy).toHaveBeenCalledWith(
`https://osf.test/download?source=file-detail&tz=${encodeURIComponent(timeZone)}`
);
openSpy.mockRestore();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { MetadataTabsComponent } from '@osf/shared/components/metadata-tabs/meta
import { SubHeaderComponent } from '@osf/shared/components/sub-header/sub-header.component';
import { MetadataResourceEnum } from '@osf/shared/enums/metadata-resource.enum';
import { ResourceType } from '@osf/shared/enums/resource-type.enum';
import { appendDownloadTrackingParams } from '@osf/shared/helpers/download-link.helper';
import { getMfrUrlWithVersion } from '@osf/shared/helpers/mfr-url.helper';
import { CustomConfirmationService } from '@osf/shared/services/custom-confirmation.service';
import { DataciteService } from '@osf/shared/services/datacite/datacite.service';
Expand Down Expand Up @@ -250,7 +251,8 @@ export class FileDetailComponent implements OnDestroy {
const storageLink = this.file()?.links.upload || '';

if (downloadUrl) {
window.open(`${downloadUrl}/?revision=${version}`)?.focus();
const link = appendDownloadTrackingParams(`${downloadUrl}/?revision=${version}`, 'file-detail');
window.open(link)?.focus();
this.actions.getFileRevisions(storageLink);
}
}
Expand All @@ -266,7 +268,7 @@ export class FileDetailComponent implements OnDestroy {
.logIdentifiableDownload(this.fileMetadata$)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe();
window.open(link)?.focus();
window.open(appendDownloadTrackingParams(link, 'file-detail'))?.focus();
}

deleteEntry(link: string): void {
Expand Down
2 changes: 1 addition & 1 deletion src/app/features/files/pages/files/files.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ describe('FilesComponent', () => {
component.downloadFolder();

expect(dataciteService.logFileDownload).toHaveBeenCalledWith('node-1', 'nodes');
expect(filesService.getFolderDownloadLink).toHaveBeenCalledWith('/v2/files/file-123/download/');
expect(filesService.getFolderDownloadLink).toHaveBeenCalledWith('/v2/files/file-123/download/', 'files');
expect(openSpy).toHaveBeenCalledWith('/v2/files/file-123/download/?zip=', '_blank');
openSpy.mockRestore();
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/features/files/pages/files/files.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ export class FilesComponent {
.logFileDownload(resourceId, resourcePath)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe();
const link = this.filesService.getFolderDownloadLink(downloadLink);
const link = this.filesService.getFolderDownloadLink(downloadLink, 'files');
window.open(link, '_blank')?.focus();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('ShareAndDownloadComponent', () => {

component.download();

expect(socialShareService.createDownloadUrl).toHaveBeenCalledWith(mockPreprint.id);
expect(socialShareService.createDownloadUrl).toHaveBeenCalledWith(mockPreprint.id, 'preprint');
expect(openSpy).toHaveBeenCalledWith('https://example.com/download');
expect(focus).toHaveBeenCalled();
expect(dataciteService.logIdentifiableDownload).toHaveBeenCalledWith(component.preprint$);
Expand Down Expand Up @@ -124,7 +124,7 @@ describe('ShareAndDownloadComponent', () => {

component.download();

expect(socialShareService.createDownloadUrl).toHaveBeenCalledWith(mockPreprint.id);
expect(socialShareService.createDownloadUrl).toHaveBeenCalledWith(mockPreprint.id, 'preprint');
expect(dataciteService.logIdentifiableDownload).not.toHaveBeenCalled();
openSpy.mockRestore();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class ShareAndDownloadComponent {
return;
}

const downloadLink = this.socialShareService.createDownloadUrl(preprint.id);
const downloadLink = this.socialShareService.createDownloadUrl(preprint.id, 'preprint');
const downloadWindow = window.open(downloadLink);

if (!downloadWindow) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('PreprintDownloadRedirectComponent', () => {
it('should redirect to download URL when id is present in browser', () => {
const redirectSpy = vi.spyOn(PreprintDownloadRedirectComponent.prototype, 'redirect').mockImplementation(vi.fn());
const { mockSocialShareService } = setup({ id: MOCK_ID });
expect(mockSocialShareService.createDownloadUrl).toHaveBeenCalledWith(MOCK_ID);
expect(mockSocialShareService.createDownloadUrl).toHaveBeenCalledWith(MOCK_ID, 'preprint');
expect(redirectSpy).toHaveBeenCalledWith(MOCK_DOWNLOAD_URL);
redirectSpy.mockRestore();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class PreprintDownloadRedirectComponent {
return;
}

const url = this.socialShareService.createDownloadUrl(id);
const url = this.socialShareService.createDownloadUrl(id, 'preprint');
this.redirect(url);
}

Expand Down
5 changes: 5 additions & 0 deletions src/app/shared/helpers/download-link.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function appendDownloadTrackingParams(link: string, source: string): string {
const separator = link.includes('?') ? '&' : '?';
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
return `${link}${separator}source=${encodeURIComponent(source)}&tz=${encodeURIComponent(tz)}`;
}
7 changes: 5 additions & 2 deletions src/app/shared/services/files.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,11 @@ describe('FilesService', () => {

it('should build folder download link with correct separator', () => {
setup();
expect(service.getFolderDownloadLink('/files/1')).toBe('/files/1?zip=');
expect(service.getFolderDownloadLink('/files/1?foo=bar')).toBe('/files/1?foo=bar&zip=');
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
expect(service.getFolderDownloadLink('/files/1')).toBe(`/files/1?zip=&source=&tz=${encodeURIComponent(timeZone)}`);
expect(service.getFolderDownloadLink('/files/1?foo=bar')).toBe(
`/files/1?foo=bar&zip=&source=&tz=${encodeURIComponent(timeZone)}`
);
});

it('should return empty reference when addons api response has no data', async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/app/shared/services/files.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { PaginatedData } from '@osf/shared/models/paginated-data.model';

import { FileKind } from '../enums/file-kind.enum';
import { ResourceType } from '../enums/resource-type.enum';
import { appendDownloadTrackingParams } from '../helpers/download-link.helper';
import { AddonMapper } from '../mappers/addon.mapper';
import { ContributorsMapper } from '../mappers/contributors';
import { FilesMapper } from '../mappers/files/files.mapper';
Expand Down Expand Up @@ -173,9 +174,9 @@ export class FilesService {
return this.jsonApiService.post<FileResponseJsonApi>(link, body);
}

getFolderDownloadLink(link: string): string {
getFolderDownloadLink(link: string, source = ''): string {
const separator = link.includes('?') ? '&' : '?';
return `${link}${separator}zip=`;
return appendDownloadTrackingParams(`${link}${separator}zip=`, source);
}

getFileTarget(fileGuid: string): Observable<FileDetailsModel> {
Expand Down
6 changes: 5 additions & 1 deletion src/app/shared/services/social-share.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ describe('SocialShareService', () => {
it('should create web urls', () => {
setup();

const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;

expect(service.createPreprintUrl('pp-1', 'osf')).toBe('https://osf.test/preprints/osf/pp-1');
expect(service.createGuidUrl('abc12')).toBe('https://osf.test/abc12');
expect(service.createDownloadUrl('res-1')).toBe('https://osf.test/download/res-1');
expect(service.createDownloadUrl('res-1')).toBe(
`https://osf.test/download/res-1?source=&tz=${encodeURIComponent(timeZone)}`
);
});

it('should generate social action items from platform config', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/app/shared/services/social-share.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ENVIRONMENT } from '@core/provider/environment.provider';

import { SOCIAL_PLATFORMS } from '../constants/social-platforms.const';
import { SOCIAL_SHARE_URLS } from '../constants/social-share.config';
import { appendDownloadTrackingParams } from '../helpers/download-link.helper';
import { SocialShareContentModel } from '../models/socials/social-share-content.model';
import { SocialShareLinksModel } from '../models/socials/social-share-links.model';
import { SocialsShareActionItem } from '../models/socials/socials-share-action-item.model';
Expand Down Expand Up @@ -56,8 +57,8 @@ export class SocialShareService {
return `${this.webUrl}/${guid}`;
}

createDownloadUrl(resourceId: string): string {
return `${this.webUrl}/download/${resourceId}`;
createDownloadUrl(resourceId: string, source = ''): string {
return appendDownloadTrackingParams(`${this.webUrl}/download/${resourceId}`, source);
}

generateSocialActionItems(content: SocialShareContentModel): SocialsShareActionItem[] {
Expand Down
Loading